Installed onEdit trigger does not

794 views Asked by At

I have a function that is intended to detect changes in one spreadsheet and copy them into another spreadsheet in the same exact cell location if the change is made in sheet1. I first tried to do this using the installed onEdit(e) function provided by sheetsAPI, but kept running into the error that I'm not authorized to open a new spreadsheet from a built-in function. I tried setting up my own trigger, but it won't activate even though I assigned an OnEdit trigger to the function.

function ChangeDetect(e){
  var range = e.range 
  var esheet = SpreadsheetApp.getActiveSheet()
  var ss = SpreadsheetApp.getActiveSpreadsheet()
   var sheet = ss.getSheets()[1];
  // literally, if the edit was made in the first sheet of the spreadsheet, do the following:
  if (esheet.getIndex() == ss.getSheets()[0].getIndex()){
    var numrows = range.getNumRows();
    var numcols = range.getNumColumns();
    for (var i = 1; i <= numrows; i++) {
      for (var j = 1; j <= numcols; j++) {
        var currentValue = range.getCell(i,j).getValue();
        var cellRow = range.getCell(i,j).getRow();
        var cellCol = range.getCell(i,j).getColumn();



        var ss3 = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1K2ifzjPTATH77tV4xInh0Ga2SuPsLdgNRSbekjDx-w8/edit#gid=0')
        var sheet3 = ss3.getSheets()[0];
        sheet3.getRange(cellRow,cellCol).setValue(currentValue)
      }
    }
  }
}
1

There are 1 answers

0
ReyAnthonyRenacia On

I think you may be experiencing the limitation/restrictions of installable triggers:

Restrictions

Because simple triggers fire automatically, without asking the user for authorization, they are subject to several restrictions:

  • The script must be bound to a Google Sheets, Docs, or Forms file.
  • They do not run if a file is opened in read-only (view or comment) mode.
  • They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the - Language service, which is anonymous.
  • They can modify the file they are bound to, but cannot access other files because that would require authorization.
  • They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions.
  • They cannot run for longer than 30 seconds.
  • In certain circumstances, add-ons for Google Sheets, Docs, and Forms run - their onOpen(e) and onEdit(e) simple triggers in a no-authorization mode that presents some additional complications. For more information, see the guide to the add-on authorization lifecycle. These restrictions do not apply to doGet() or doPost().