如果从onEdit()调用,则使用openById()的方法不起作用

时间:2018-07-22 12:36:44

标签: google-apps-script google-sheets triggers

我想在另一个界面电子表格中使用数据库电子表格的数据。我有一个copySelectedRow()函数,如果我从onEdit()调用它就不会起作用,但是如果我从其他地方(例如incrementByOne())调用它,它将起作用。

//runs if the id cell is modified
function onEdit(e)
{ 
  var range = e.range;
  var columnOfCellEdited = range.getColumn();
  var rowOfCellEdited = range.getRow();

  if ( columnOfCellEdited === 2 && rowOfCellEdited === 1 )
  {
    Logger.log( "triggered" );
    copySelectedRow();
    chooseGroupForCollisionCheck();
  }
};

//-----------------------------------------------------------------------------

//increment the value of the id by one
function incrementByOne()
{ 
  var app = SpreadsheetApp;
  var targetSheet = app.getActiveSpreadsheet().getActiveSheet();
  var tempNumber = targetSheet.getRange( 1, 2 ).getValue();
  targetSheet.getRange( 1, 2 ).setValue( tempNumber + 1 );

  copySelectedRow();
  chooseGroupForCollisionCheck();
}

//-----------------------------------------------------------------------------

//copy the chosen row to the appropriate cells of the editor sheet
//if zero sets the cells to default
function copySelectedRow()
{ 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  Logger.log( "copystart " + ss.getName() );
  var shEdit = ss.getActiveSheet();

  var spreadSheetData = SpreadsheetApp.openById( "1koyd1dIl_SLS5XAc26UO-SBOL9d9Thfd83xzIc900fo" );
  Logger.log( "database " + spreadSheetData.getName() );
  var shData = spreadSheetData.getSheets()[ 0 ];

      var idNumber = shEdit.getRange( 1, 2 ).getValue();
      (...)
}

图片显示日志输出。同样,如果我调用incrementByOne(),则日志很好,该函数复制给定的行,但是如果调用onEdit(e),则程序将停止,然后转到“数据库”日志。 我不知道为什么,请帮忙:)

致电incrementByOne()

后登录

致电onEdit(e)

后登录

0 个答案:

没有答案