简体   繁体   中英

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

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<\/a>

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.

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);
    }
  }
}

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 fire a function via a trigger the function gets always the 'event object' as the only argument. In this case you actually run DeleteRow(e) instead of DeleteRow() . Where e is the event object. It's not undefined . Thus the condition if (typeof(column) != 'undefined') is false and at the same time the variable column is not a number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM