I'm trying to come up with an IF statement that will trigger my macro to run based on a specific value in one cell. My Spreadsheet has many tabs. This is for a forecasting template.
Here is what I have come up with but I am running out of ideas..
function Sum_Calcs() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Integrity Check'), true);
spreadsheet.getRange(K20).getValue(getActiveRange(), true);
if(activeRange === "1") {
}
//My Script is then located beneath//
Any help is much appreciated!
You need to
implement an onEdit function to re-evaluate the if statement each time the value in "K20" was changed (please note that it will work automatically only if the value has been changed manually, by a human)
A second function (macro) that will be called within the if statement if your condition is fulfilled
Based on your code snippet above there some things you need to change:
When you use the method getValue() to retrieve the value of a cell - do not specify any parameter within the brackets () of the method. Instead, assign the return value of this method to a variable
If you want to retrieve a cell value (or perform any other operation) in a certain sheet, use getSheetByName() instead of setActiveSheet
To compare within an if statement the actual value against an integer (number) use if(activeRange == 1)
Note that when getting a range in A1 notation you need to use quotes ("")
Sample code based on your situation:
function myForecastMacro(){
// specify here what shall happen if your "if" statement is fulfilled
function onEdit() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Integrity Check');
var activeRange=sheet.getRange("K20").getValue();
if(activeRange == 1) {
myForecastMacro();
}
}
Related
I need function to simply trigger another function if a specific Google Sheet column's value (or values) is "START".
Something like this:
If column AUTO!N2:N is "START", then trigger the function letsStart()"
If column AUTO!N2:N is not "START", then do nothing.
Simple as that :)
Is this possible?
The letsStart() function already ends with:
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AUTO")
.getRange('N2:N')
.createTextFinder('START')
.replaceAllWith('STOP')
...and working fine.
This is to avoid unintended looping.
I believe your goal is as follows.
You want to run a function of letsStart() when the cells AUTO!N2:N includes a value of "START".
In this case, how about the following 2 patterns?
Pattern 1:
In this pattern, when a cell in AUTO!N2:N is edited and the cell value is "START", letsStart() is run.
function onEdit(e) {
const { range, source } = e;
const sheet = range.getSheet();
const value = range.getValue();
if (sheet.getSheetName() != "AUTO" || range.columnStart != 14 || range.rowStart == 1 || value != "START") return;
letsStart();
// or range.setValue("STOP");
}
In this sample, a simple trigger of OnEdit is used. So, when you use this script, please edit a cell of AUTO!N2:N. By this, the script is run. When you put "START" to AUTO!N2:N, letsStart() is run.
Please be careful about this.
In this case, I thought that range.setValue("STOP"); might be able to be also used instead of letsStart();.
Pattern 2:
In this pattern, when the cells AUTO!N2:N includes a value of "START", letsStart() is run.
function myFunction() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AUTO");
const lastRow = sheet.getLastRow();
if (lastRow == 0) return;
const textFinder = sheet.getRange("N2:N" + lastRow).createTextFinder("START").matchEntireCell(true);
if (!textFinder.findNext()) return;
letsStart();
// or textFinder.replaceAllWith("STOP");
}
When this script is run, when the cells AUTO!N2:N includes a value of "START", letsStart() is run. Althought I'm not sure your whole script of letsStart(), when I saw your script of letsStart(), I thoug that in this case, I thought that letsStart(); might be able to be replaced with textFinder.replaceAllWith("STOP");.
For example, when myFunction is modified to onEdit, this script can be also used with the simple trigger of OnEdit.
References:
createTextFinder(findText) of Class Range
Simple Triggers
I have a values in google sheet and the format is =+40,-58. This give me ERROR! because the sheet is taking it as formula.
I can manually edit this by adding ' single qoute before equal sign but when i append qoute using script it append qoute with ERROR!.
Tried multiple thing like getting cell type, convert it to string.
Tried set formula method but it appends another equal sign before the cell value
please check the code below
if (//my condition){
sheet.getRange(i,col_in+1).setValue("'"+colvalue)
I am looking for possible solutions like, how can I get the actual value of the cell from fx
or
How can i append a single quote with the cell value instead of appending quote with ERROR.
please see the screenshot of the sheet
Descrition
Because the formula is giving "#ERROR" you need to getFormula and use setValue
Script
function test() {
let cell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A6");
let value = cell.getFormula();
if( value !== "" ) {
console.log("formula = "+value);
cell.setValue("'"+value);
}
}
Console.log
7:30:31 AM Notice Execution started
7:30:31 AM Info formula = =+52,-64
7:30:32 AM Notice Execution completed
I'm trying to make a script that, whenever triggered, runs through each row checking if the checkboxes in column F are checked. If so, the content of column A in that row is replaced with a 0. Upon running the code, nothing happens. I am not sure where the script gets caught up, but I would assume it's in the for() loop. I have also included a picture of the spreadsheet itself for easier understanding. Any help is appreciated.
My code:
function onEditButAgain(event){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 6;
if(editedCell.getColumn() == columnToSortBy)
{
for(var x = 2; x<=100; x++)
{
var checked = sheet.getRange(x, 6);
if(checked.isChecked == true)
{
var date = sheet.getRange(x, 1);
date.setValue("0");
}
}
}
}
The image of the spreadsheet itself:
Explanation:
Your goal is to manually execute a script that will change the value in column A whenever the cell in the same row in column F is checked.
The obvious issue in the code is isChecked which should be isChecked(). You use parenthesis when you want to execute a function and in this case you want to execute isChecked.
In this case, an onEdit script will be redundant. If you want to use an onEdit trigger with the name onEditButAgain you have to create an installable trigger. But you don't need an installable trigger for this purpose.
Although you can wrap up the following code in this answer in a simple onEdit() function, the event object is not used and the code won't be 100% efficient. But if you want to run it manually, you don't need a trigger anyway.
When you use a loop, it is not a good practice to use methods like getRange or isChecked or setValue because these methods are computationally expensive and your script will very slow. Work with arrays instead and getValues/setValues.
Solution:
function myFunction(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1'); // change it to your sheet name
const valA = sh.getRange('A2:A'+sh.getLastRow()).getValues().flat();
const cb = sh.getRange('F2:F'+sh.getLastRow()).getValues().flat();
const new_valA = cb.map((c,i)=> [c ? "0" : valA[i]]);
sh.getRange(2,1,new_valA.length,new_valA[0].length).setValues(new_valA);
}
I am currently trying to set up a simple calculation in google sheets that will only add when the value is not equal to the previous value. How can I go about this? All I need is to get the previous value in from the input. It has been a while since I have given my hand at JavaScript so I could be forgetting something simple like that.
I have tried looking into the google API and looking to similar solutions for help. I could just be dumb or missing something simple but does anyone have any idea?
///Call onEdit that takes in the value of e
function onEdit(e)
{
//Get the input value and its previous value
var input = e;
var previous = e; //.prev();? .offset??
//Check if cell is updated
if ( input != previous )
{ return true; }
else
{ return false; }
}
That way
function onEdit(e){
Logger.log(e.oldValue);
}
More details on the event object
You function could be summarised as:
function onEdit(e){
return e.oldValue != e.value;
}
However, I believe that if you just put the same value back in the cell, it's not going to be considered an edit, and thus won't trigger the onEdit() function. So this will always return true when it runs, and not run at all in all other cases.
I want to use the "check box" operation of google sheets, and when the check box is true, then call to a function.
Someone can help me with that ?
Supposing your checkbox is at A1, you could use this script to test when it's checked or unchecked (modified):
function onEdit(e) {
var range = e.range;
if (range.getA1Notation() == 'A1') {
var value = range.getValue();
range.setNote('Changed on ' + new Date() + ' to ' + range.getValue());
if (typeof value === 'boolean' && value == true) {
// Do something or call a function when the tick box is checked
}
}
}
While I'm unsure of exactly what you want to do in javascript, the Google Script editor may be useful for you.
In a Google Sheet, go to Tools > Script Editor. It should open a new page and create a new project with a blank function. This is where you can make new functions to be run within Google Sheets.
As an example I made a function called getSum:
function getSum(a,b) {
return a+b;
}
If you save this script and go back to Sheets, you can do =getSum(1,2) and it will return 3
If you wanted to integrate this with a Tick Box, you could do =IF(A1,getSum(1,2),getSum(2,2))
In this case, when the tick box is checked, it will run the first statement, and return 3 , when the box is unchecked, it will return 4
I'm not entirely sure on what you are trying to achieve with JavaScript, but this is one way to introduce custom functions (using Google Script).