Home > Mobile >  Google Apps Script only works when manually triggered, not by any other trigger
Google Apps Script only works when manually triggered, not by any other trigger

Time:02-11

I'm a bit struggling to understand this matter: My code meant to delete rox X if the colour of a cell in row X column Y is RED (f4c7c3). For example :

Delete Row 7

The code works great thanks to a few questions I saw around here that helped me make it better, but it will only work when I click run. If I'm using any trigger (I want to use a time-driven trigger every day between midnight and 1 AM, but I've tried them all), it shows that it was completed, but nothing happens to the spreadsheet. Executions

Here is my code, I'd appreciate your help with that issue:

function DeleteRow(column,color) {   //Delete row if the color in the Date column is F4C7C3
  var column = (typeof(column)!='undefined')?column:4;  
  var color = (typeof(color)!='undefined')?color:'#f4c7c3'; 
  var ss = SpreadsheetApp.openById("1Ichlawob5EHzSK4ljRSJfViugFWugTzHOMId3q5LL6w");
  var sht = ss.setActiveSheet(ss.getSheets()[0]); 
  var rng = ss.getDataRange();
  var rngA = rng.getBackgrounds()
  for(var i=rngA.length-1;i>-1;i--)
  {
    if(rngA[i][column-1]==color)
    {
      sht.deleteRow(i 1);
    }
  }
}

BTW, just got a summary that shows the error even though the executions indicate successfully-

Errors

CodePudding user response:

Try to replace the line:

var column = (typeof(column)!='undefined')?column:4;  

To:

var column = (typeof(column)=='number')?column:4;  

Or just:

var column = 4;  

Explanation

Whenever you're firing a function via a trigger the function gets an 'event object' as a first and only argument. You're actually calling DeleteRow(e) instead of DeleteRow(). Where the e is the event object. It's not undefined and its not a number.

  • Related