复制更改后的最后一行-同时输入

时间:2019-05-26 13:05:43

标签: google-apps-script

我有一个google脚本,每次输入时都会将该电子表格的最后一行复制到另一个电子表格。该条目根本没有链接到Google表单-因此它没有绑定到onFormSubmit触发器-此提交是从JotForm表单进行的,并自动插入到电子表格中。我想知道即使两个用户同时提交他们的代码,我的代码是否仍能正常工作。

代码工作正常-似乎没有问题。我使用On change触发器。但是,我想确保它始终有效。非常感谢您的提前帮助。

function copyLastRow() {
  var target = SpreadsheetApp.openById('xxxxx').getSheetByName('Records');
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
 var lastrow = sheet.getLastRow();
 var sourceData = sheet.getRange(lastrow, 1, 1, 10).getValues();
 target.appendRow(sourceData[0]);
}

1 个答案:

答案 0 :(得分:0)

这是我想您可以用来完成此目标的模板。

function updateSpreadsheet() {

  //Open Form Responses Spreadsheet and get the Sheet you are reading data from
  // You will need to update ID and SheetName parameters for your own sheets.
  var formResponses = SpreadsheetApp.openById(ID).getSheetByName(SheetName);

  //Open destinationSheet that you are writing to and get the sheet that you are writing to
  // You will need to update ID and SheetName parameters for your own sheets.
  var destinationSheet = SpreadsheetApp.openById(ID).getSheetByName(SheetName);

  //Getting last rows from formResponse and destinationSheet
  //It is not totally necessary to create everything as a variable, but I wanted to make it explicit what I am doing here
  var formResponsesLastRow = formResponses.getLastRow();
  var destinationSheetLastRow = destinationSheet.getLastRow();

  //We are going to calculate the difference between the formResponse and destinationSheet 
  //This lets us know how many more rows have been added since we last updated destinationSheet
  var numberNewRows = formResponsesLastRow - destinationSheetLastRow;


  //Now get the new data to Write  
  var dataToWrite = formResponses.getRange((formResponsesLastRow - numberNewRows) + 1 , 1, numberNewRows, 3).getValues();

  //Write to destinationSheet
  destinationSheet.getRange(destinationSheetLastRow + 1, 1, numberNewRows, 3).setValues(dataToWrite);



}