I'm trying to take a set of names with check boxes next to them and make a system so that you can check some of the names (mark them as "True") and click a button. It would then increment +1 the value next to the names of the people marked true.
Here is a link to a sample sheet:
https://docs.google.com/spreadsheets/d/1gf-BrXXR0cAYCn7bMkvvK65R290NXbP9D6aA68c06C8/edit?usp=sharing
If column A, row 2 (Tim's row) is marked true, I want to increment the value in column C, row 2 by one, so Tim would have a running total of tardies next to his name.
I hope this is do-able. Thanks!
(Now I know what you're trying to get)
In order to increment a value via the press of a button, as far as I know you have to use scripts (Tools -> Script Editor). Here's something I threw together:
// editCell takes the cell to edit and it's new value
function editCell(cellName, value) {
SpreadsheetApp.getActiveSheet().getRange(cellName).setValue(value);
}
// getCell takes the cell's value and returns it
function getCell(cellName) {
return SpreadsheetApp.getActiveSheet().getRange(cellName).getValue();
}
// plusOne adds one to the field supplied. It's linked to the button in the sheet
function plusOne() {
editCell("C2",getCell("C2")+1);
}
In order to make it work, you may need to change the targeted Cell (currently C2). You'll also need to create a drawing (Insert -> Drawing) which will act as the button you'll be able to press. Once inserted, click on the three dots on it and click on Link Script. Type in plusOne. When executing it the first time, it'll ask you to authenticate the use of scripts.
That should do the trick. I hope you have some understanding of Java Script though (to modify the code to your needs optimally).
Edit - Expandable version
So, to make every number behind a ticked field increase by one, you can use this version of the code:
// Adds one to every field within "AddArea" that has a tick in front of it. It's linked to the button in the sheet.
function plusOne() {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange("AddArea");
var values = range.getValues();
var newValues = [];
for (var i = 0; i < range.getNumRows(); ++i) {
var row = values[i];
if(row[0]) {
newValues.push([true, row[1]+1]);
}
else {
newValues.push([false, row[1]]);
}
}
range.setValues(newValues);
}
You need to define a custom named area, named "AddArea" (Data -> Labeled Areas [or similar]), link the script to a button and allow the script to be run. This was hard but very fun to figure out.
Example Sheet for reference (updated)
Can be achieved with just, for example for C2:
=A2+C2
but you would need to turn on iterative calculation (File > Spreadsheet settings... > Calculation [Max. 1 is adequate]) and I would not really recommend that over a trigger with Google Apps Script.
Related
I created a custom function for google spreadsheets. All it does is return a random letter. The function works great when I first enter it into a cell. But now I want to be able to "recalculate" the function using a keyboard shortcut; I'd also be willing to refresh the page if needed.
TLDR: I want to be able to hit a key and have my custom functions recalculate.
How can I accomplish this?
Edit to add:
Here is the code for my function.
//returns a random letter suitable for use in function notation
function ranFunLet() {
var letters = ['a','b','c','d','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z']
var letter = letters[Math.floor(Math.random()*letters.length)];
//console.log(letter);
return letter;
}
I would like the cell I use it in to run the function again when I press a button (or refresh the page).
Suggestion
The Apps Script editor does support keyboard shortcut trigger as per this existing answer. However, you may want to try Importing functions as macros, then you can assign a unique keyboard shortcut to it.
Here's a sample
Sample Sheet
Sample script function to test
This sample script function increments the number on A1 cell.
function sample() {
var data = SpreadsheetApp.getActive().getActiveSheet().getRange("A1").getValue();
var res = data+1;
SpreadsheetApp.getActive().getActiveSheet().getRange("A1").setValue(res);
}
Import the function on your spreadsheet (in my testing it is named as sample):
In the Google Sheets UI, select Tools > Macros > Import.
Select a function form the list presented and then click Add
function.
Select clear to close the dialog.
Select Tools > Macros > Manage macros.
Locate the function you just imported in the list. Assign a unique
keyboard shortcut to the macro. You can also change the macro name
here; the name defaults to the name of the function.
Click Update to save the macro configuration.
Result
After pressing the sample shortcut key Ctrl + Alt + Shift + 2, the function incremented the number on A1 cell from 1 to 2:
NOTE: You can not choose a specific shortcut & if you'll edit your function on the Apps Script editor, you would need to re-import your function again as a macro.
the button solution is done like this:
https://www.youtube.com/watch?v=yaBMsSpAxYM
I have a table in Google Sheets in the format:
A B C
Day Date inventory demand
Day2 Date2 inventory demand
etc.
Others are required to fill in inventory and demand every day. Thus, it would be helpful if they open the sheet they jump always to the current date. This could be done over HYPERLINK or code. However, as I am informed onOpen works for the editor, however not for viewers. As this is currently the case. When I open the file I jump to the current date, however people viewing and editing the file per link do not.
Could somebody please help me? Thank you.
I also do not understand, why creating a cell that jumps to the current date as an alternative does not work.
I tried various variations of
=HYPERLINK("l i n k&range=B"&MATCH("TODAY",B1:B1500,0),"Jump to today")
or
=HyperLink("LINK&range=B" &Match(Today(),B6:B,1),"JUMP to Today")
// jump to current date
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("B:B");
var values = range.getValues();
var day = 24*3600*1000;
var today = parseInt((new Date().setHours(0,0,0,0))/day);
var ssdate;
for (var i=0; i<values.length; i++) {
try {
ssdate = values[i][0].getTime()/day;
}
catch(e) {
}
if (ssdate && Math.floor(ssdate) == today) {
sheet.setActiveRange(range.offset(i,0,1,1));
break;
}
}
}
try like this:
=HYPERLINK("#gid=0&range=B"&MATCH(TODAY(); B6:B; 0)+5; "zu heute")
I found the options: Edit Triggers
To manually create an installable trigger through a dialog in the script editor, follow these steps:
From the script editor, choose Edit > Current project's triggers.
Click the link that says: No triggers set up. Click here to add one now.
Under Run, select the name of function you want to trigger.
Under Events, select either Time-driven or the Google App that the script is bound to (for example, From spreadsheet).
Select and configure the type of trigger you want to create (for example, an Hour timer that runs Every hour or an On open trigger).
Optionally, click Notifications to configure how and when you are contacted by email if your triggered function fails.
Click Save.
Google Explanation
hello i need some help debugging this simple script i compiled to TRY and reset cells F3 all the way down to F338 in google spreadsheets.
function ClearRange()
{
if('H20' => 1)
{
sheet.getRange('F3:F338').clearContent();
sheet.clearcontent('H20');
}
}
more or less i want it to run on edit, if on edit cell H20 is greater then one (would be awesome if it was anything other then blank but im unsure how to do that) then it would set the range to blank cells. i would then like it to reset cell H20 back to blank or 0 then end the script.
To return the value of a cell, use something like this:
var sheet = SpreadsheetApp.getActiveSheet();
var cellValue = sheet.getRange('H20').getValue();
You need to manually declare the sheet variable if you want to use .getRange().getValue() or .getDataRange() and a bunch of other stuff.
Also note that just typing in 'H20' doesn't return the cell value, it's just interpreted as a string of text.
To detect if H20 is anything other than blank, you could test if the cell value's length is not 0:
if(sheet.getRange('H20').getValue().length != 0)
To also do a .clearContent() on cell H20, you'll need to do the same thing you did with cells F3:F338:
sheet.getRange('H20').clearContent();
To make the function run on an edit, click on the clock-like icon in the script editor, next to the save icon, and create a new trigger for ClearRange.
Firstly excuse me for my lack of knowledge.
Let me explain my current situation.
I have 3 Tabs on one Spreadsheet:
A master list
Types of Stock
Stock Requisition sheet
The master list simply gathers the information from the Stock Tab.
The Stock requisition gathers the information from Master list.
I currently have the following simple script that triggers based upon an edit.
function myFunction() {
MailApp.sendEmail(
"excessstock#greenlifepharma.co.uk",
"Excess Stock - Someone Added Stock ",
"Please see changes on main list of spreadsheet");
}
These are different triggers that i need...
A trigger for my function upon edit of the Stock sheet column C.
A different Trigger for when stock requisition sheet is edited.
Hope i explained well.
Very easy to do what you require. Copy and paste the code below and set an onEdit() trigger.
NOTE: In the IF statment I have 'Stock' and 'Stock Requisition', these must be exact matches to the sheet names i.e. capitalisation and spacing.
function myFunction(e) {
var getSheet = e.source.getSheetName();
var getRange = e.range.getColumn();
if(getSheet == "Stock" && getRange == 3 || getSheet == "Stock Requisition"){
var stockEmail = "---------";
var stockReqEmail = "----------";
MailApp.sendEmail(
(getSheet == "Stock") ? stockEmail : stockReqEmail ,
"Excess Stock - Someone Added Stock ",
"Please see changes on main list of spreadsheet");
}
}
I'm trying to have a timestamp appear in a column whenever data is added to a sheet. I've had some success with the following script:
function onEdit(e) {
var colToWatch = 2, colToStamp = 1;
if (e.range.columnStart !== colToWatch) return;
var writeVal = e.value ? new Date() : '';
e.source.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.setValue(writeVal);
}
My issue is, every time the text in col 2 is edited, the timestamp changes to the current time.
My hope it to have a timestamp that shows when the text was originally added (so it can be organized by that date in another sheet). Other people will have access to this sheet and may change something by accident and cause changes in the sheet organized by date.
I'm new to scripting, is it possible to have an onEdit only run the first time data is added? It seems like onChange() might be able to help me, but I haven't been able to find anything.
Basically you want to terminate if the timestamp cell is already filled.
if (e.source
.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.getValue()) {
return;
}