问题
The code below copies range and moves to next Spreadsheet in same workbook called "Topup Required".
I would like it to send this data to another spreadsheet inside another workbook instead.
function onEdit(event) {
// assumes source data in sheet named PrepSheet
// target sheet of move to named TOP UP NEEDED
// getColumn with check-boxes is currently set to column I
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "PrepSheet" && r.getColumn() == 9 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("TOP UP NEEDED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
I tried replacing
var targetSheet = ss.getSheetByName("TOP UP NEEDED");
with
var targetSheet = SpreadsheetApp.openById("sheet ID").getSheetByName("TOP UP NEEDED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
but it didnt work
Could anyone tell me where i went wrong
Thanks
回答1:
- You want to copy the values and format from
s.getRange(row, 1, 1, numColumns)to the last row on the sheet ofTOP UP NEEDEDin the Spreadsheet ofsheet ID, when the cell is edited.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification point:
Unfortunately, in the current stage, copyTo() of Class range cannot be used for other Spreadsheet.
Pattern 1:
In this pattern, copyTo() is used. So both the values and the cell format are copied.
Modified script:
When your script is modified, please modify as follows.
From:var targetSheet = ss.getSheetByName("TOP UP NEEDED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
To:
var targetSS = SpreadsheetApp.openById("sheet ID");
s = SpreadsheetApp.getActiveSheet();
var tempSheet = s.copyTo(targetSS);
var targetSheet = targetSS.getSheetByName("TOP UP NEEDED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
tempSheet.getRange(row, 1, 1, numColumns).copyTo(target);
targetSS.deleteSheet(tempSheet);
Pattern 2:
In this pattern, getValues() and appendRow() are used. So only the values are copied.
Modified script:
When your script is modified, please modify as follows.
From:var targetSheet = ss.getSheetByName("TOP UP NEEDED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
To:
var targetSheet = SpreadsheetApp.openById("sheet ID").getSheetByName("TOP UP NEEDED");
var sourceValues = s.getRange(row, 1, 1, numColumns).getValues()[0];
targetSheet.appendRow(sourceValues);
Note: IMPORTANT
In this modification, the simple trigger of
onEdit()cannot be used. An error occurs atopenById(). So please use the installable trigger of OnEdit event trigger. In this case, please modify the function name as follows.From
function onEdit(event) {To
function installedOnEdit(event) {After this modification, please install the OnEdit event trigger to installedOnEdit as the installable trigger. By this, when the cell is edited,
installedOnEditis run without the double executions. Please be careful this.
References:
- copyTo(destination) of Class Range
- copyTo(spreadsheet) of Class Sheet
- getValues()
- appendRow(rowContents)
- Simple Triggers
- Installable Triggers
If I misunderstood your question and this was not the direction you want, I apologize.
来源:https://stackoverflow.com/questions/59462298/modify-code-copy-range-to-another-spreadsheet-google-sheets