0

I have a boolean value on Spreadsheet Cell A1 and some content on B1 which I want to get cleared based on the boolean value BUT after 15 minutes of matching the boolean value.

Following is the code which I had created based on my research all over the Internet.

  function boolean(value) {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var value = sheet.getRange('A1:A6').getValue();
if (value=="Yes") {trigger15()};
}

function trigger15() {
  ScriptApp.newTrigger("trigger15")
   .timeBased()
   .everyMinutes(15)
   .create();
   {clearRange()}
 }

function clearRange() {
  //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('B1:B6').clearContent();
}

The problems I'm facing:

  • Instead of working after 15 minutes of condition matching, the trigger15 scripts creates many "15 minutes triggers" which eventually shows an error of "Too many triggers"
  • The B1 cells get erased within 10 seconds and even if the A1 is not "Yes"
  • One more thing, I'm struggling to do is: how can I use a single app script to apply this rule to further rows like A2 & B2, A3 & B3... So on.

What correction should I do to the script to make it work perfectly?

I recently started using App script so knows very basics. It would be helpful of you provide some resources for further digging.

1 Answer 1

0

Instead of working after 15 minutes of condition matching, the trigger15 scripts creates many "15 minutes triggers" which eventually shows an error of "Too many triggers"

In your code,

function trigger15() {
  ScriptApp.newTrigger("trigger15")
    .timeBased()
    .everyMinutes(15)
    .create();
    {clearRange()}
  }

You are creating a trigger for that same trigger-building-function to run (recursively) every 15 minutes.

The newTrigger() method takes functionName as a parameter, so your trigger creation function should be:

function trigger15() {
  ScriptApp.newTrigger('clearRange')
           .timeBased()
           .everyMinutes(minutes)
           .create();
}

The B1 cells get erased within 10 seconds and even if the A1 is not "Yes"

Every time you call the trigger15 function (as you have currently written it), you are unconditionally clearing the content of cells B1:B6. See the last line of that function - {clearRange()}

One more thing, I'm struggling to do is: how can I use a single app script to apply this rule to further rows like A2 & B2, A3 & B3... so on.

In my opinion, the best approach would be to create a custom formula that handles single cells, and use that on the spreadsheet (frontend) in whichever cells you need it. This is much more scalable and likely less overhead than creating a single Apps Script acting blindly on a spreadsheet.

2
  • Thanks a lot. Is it possible to write a custom function to setValue in the cells? For eg: I had created a custom function and run it on A1. So is it possible to set a value in cell B1 by executing a custom function on any other cell (i.e. A1 in this case)? Dec 16, 2016 at 8:58
  • One more thing I want to achieve is to record a value from a cell and set that value in another cell. This thing is not possible with custom function because custom function restricts to make changes in another cell (except in array results). How can record and set a value in all the rows in my sheet by using app script? Dec 16, 2016 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.