Google apps script Google应用程序脚本触发不将数据写入同一电子表格中的其他工作表

Google apps script Google应用程序脚本触发不将数据写入同一电子表格中的其他工作表,google-apps-script,Google Apps Script,我有以下应用程序脚本与接受谷歌表单数据的谷歌电子表格关联: function writePatientData() { var spreadsheet = SpreadsheetApp.openById("<spreadsheet id>"); var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]); //get last row in active/main sheet var n

我有以下应用程序脚本与接受谷歌表单数据的谷歌电子表格关联:

function writePatientData() {
  var spreadsheet = SpreadsheetApp.openById("<spreadsheet id>");
  var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);

  //get last row in active/main sheet
  var numRows = sheet.getLastRow();

  //get last row of data
  var last_row = sheet.getSheetValues(numRows, 1, 1, 23);

  //get patientID (column V) in last row of sheet
  var lastPatientID = sheet.getRange(numRows,3).getValue();

  //find patient sheet based on patientID and make it active, then write to it
  var patientSheet = SpreadsheetApp.getActive().getSheetByName(lastPatientID);
  var activePatientSheet = SpreadsheetApp.getActive().getSheetByName(lastPatientID);
  activePatientSheet.getRange(activePatientSheet.getLastRow()+1, 1,1,23).setValues(last_row);
}
函数writePatientData(){
var电子表格=SpreadsheetApp.openById(“”);
var sheet=SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
//获取活动/主工作表中的最后一行
var numRows=sheet.getLastRow();
//获取最后一行数据
var last_row=sheet.getSheetValues(numRows,1,1,23);
//获取工作表最后一行的patientID(第V列)
var lastPatientID=sheet.getRange(numRows,3.getValue();
//根据patientID查找患者工作表并将其激活,然后写入
var patientSheet=SpreadsheetApp.getActive().getSheetByName(lastPatientID);
var activePatientSheet=SpreadsheetApp.getActive().getSheetByName(lastPatientID);
activePatientSheet.getRange(activePatientSheet.getLastRow()+1,1,23).设置值(最后一行);
}

此脚本所做的是基于patientID(第V列)将数据(一行)写入此电子表格中的另一个工作表。当我手动运行脚本时,它会正常工作。然而,当我设置一个触发器来运行这个脚本(无论是onSubmit还是edit)时,什么都不会发生。我创建了另一个函数,它只向日志中写入一条消息,并为该函数设置一个触发器,它就工作了,所以我认为脚本中有某种东西导致它失败。任何想法都值得赞赏。

您的代码存在一些问题。我试图在评论每一行我修改的内容时修复它。希望这是足够清楚,请评论,如果你有任何问题,我会试图澄清

function writePatientData() {
  var spreadsheet = SpreadsheetApp.getActive(); //no need for id if the script is on the same spreadsheet
  //var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
  //setActiveSheet will not work from a trigger like on-form-submit (what if no-one has the sheet open, or multiple have)
  var sheet = spreadsheet.getSheets()[0]; //if you want the first sheet, just get it, no need to "activate"

  var numRows = sheet.getLastRow();

  var last_row = sheet.getSheetValues(numRows, 1, 1, 23)[0]; //added [0] since it is just one row

  //var lastPatientID = sheet.getRange(numRows,3).getValue(); //you already have this in memory
  var lastPatientID = last_row[2]; //arrays are zero based, that's why 2 instead of 3
  //btw, you mention column V, but this is actually C

  //var patientSheet = SpreadsheetApp.getActive().getSheetByName(lastPatientID);
  //you already have the spreadsheet, no need to get it again
  var patientSheet = spreadsheet.getSheetByName(lastPatientID);
  //var activePatientSheet = spreadsheet.getSheetByName(lastPatientID); //this is the exact same as above, why?
  patientSheet.appendRow(last_row); //appendRow is just simpler than getRange(getLastRow).setValues
}

你在Stackdriver日志中看到了什么错误?你能分享你的电子表格吗?我没有使用Stackdriver(我想我应该是)@JSmith,添加了一个指向spreadsheetStackdriver的链接,它将记录执行期间发生的错误。触发函数(可安装或简单)不会在UI中显示错误-它们只能在其他位置记录异常(即到Stackdriver)。当您使用openById()时,是否有活动工作表?活动图纸是正在显示但未显示的图纸。没有显示,轰!你是达曼·亨里克。我爱它,它是开箱即用的。看起来setActiveSheet是触发器不起作用的原因,但是请欣赏带有注释的编辑。