简体   繁体   中英

How to copy data from one spreadsheet to another on edit

I'm trying to program a spreadsheet so that whenever I add a new entry to it, it copies the data from that cell into another spreadsheet. Here's my code (that doesn't seem to work):

function onEdit(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var target = SpreadsheetApp.openById("*************");
  var target_sheet = target.getSheetByName("Dashboard");
  var last_row = target_sheet.getLastRow();
  target_sheet.insertRowAfter(last_row);
  var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));
  e.range.copyTo(target_range);
}

Any ideas what I'm doing wrong?

There are two types of triggers in Apps Scripts - simple triggers and installable triggers. onEdit() is an example of a simple trigger (the name is implicit). And there are limits to what you can do inside a simple trigger. And that includes opening another spreadsheet.

So, coming to the solution. Rename your function to something else, say onEdit1() . And set up an installable trigger manually and set it to run each time the spreadsheet is edited. Hint: Use Resources --> Current project's triggers from the script editor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM