Copy data to a separate file when submitted

旧巷老猫 提交于 2021-01-28 21:48:48

问题


I'm wondering if when submitted, the data can be pasted to another separate file (and not tab). Do you think that there's a way?

You can access the file here : https://docs.google.com/spreadsheets/d/1FFlsfFyVyQobnbfAgQZA_r5IQeBhgOdK4cBpi5yo77U/edit?usp=sharing

Following code:

function myFunction(){  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var inputS = ss.getSheetByName("Input");
  var outputS = ss.getSheetByName("Output");
  var inputRng = inputS.getRange("A2:I2");
  var values = inputRng.getValues();
  inputRng.clearContent(); // clear input range       
  outputS.getRange(outputS.getLastRow()+1, 1, 1, values[0].length).setValues(values);
}

回答1:


Explanation:

You can use openById(id) to access a different spreadsheet file by its id. As the documentation states:

For example, the spreadsheet ID in the URL https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 is "abc1234567".

and assuming that the target spreadsheet has a sheet named "Output" then the following code will work. Otherwise, change it to the name of the target sheet.

Solution:

function ScriptBS() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ts = SpreadsheetApp.openById(id); // put here the id of the target spreadsheet file
  var inputS = ss.getSheetByName("Input");
  var outputS = ts.getSheetByName("Output"); // get sheet Output of the target spreadsheet file
  var inputRng = inputS.getRange("A2:I2");
  var values = inputRng.getValues();
  inputRng.clearContent(); // clear input range       
  outputS.getRange(outputS.getLastRow()+1, 1, 1, values[0].length).setValues(values);
}


来源:https://stackoverflow.com/questions/65583185/copy-data-to-a-separate-file-when-submitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!