问题
In google sheets, I'm looking to have data copied from one sheet to another after editing a particular cell in the sheet being copied. I have code that does what I want except for only being trigger by the particular cell. Can anyone tell me what I'm missing?
function OnEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destination = SpreadsheetApp.openById("1P6gj2UwfRmSMYZ4LBovvqbqy88aluOysPqWWhWbw_VE")
var source_sheet = ss.getSheetByName("Submit");
var destination_sheet = destination.getSheetByName("Point Tracker");
var source_range = source_sheet.getRange("A2:C2");
var editedcell = source_sheet.getRange("C2");
if(editedcell.getValue() !== 0){
var last_row = destination_sheet.getLastRow();
destination_sheet.insertRowAfter(last_row);
var destination_range = destination_sheet.getRange("A"+(last_row+1)+":C"+(last_row+1));
source_range.copyTo(destination_range,{contentsOnly:true});
source_sheet.getRange("C2").setValue("");
}
}
回答1:
See if this works
function onEdit(e) {
if (e.source.getActiveSheet().getName() !== "Submit" ||
e.range.getA1Notation() != 'C2' || typeof e.value == 'object') return;
SpreadsheetApp.openById("1P6gj2UwfRmSMYZ4LBovvqbqy88aluOysPqWWhWbw_VE")
.getSheetByName('Point Tracker')
.appendRow(e.range.offset(0, -2, 1, 3)
.getValues()[0]);
e.range.setValue(null);
}
Note: this script uses an event object. Don't test the script by clikcing the play button in the script editor. Instead edit the acual cell and see if it works.
回答2:
I think this might work. I usually have to debug these things but hopefully I got you closer.
function copyOnTrigger(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destination = SpreadsheetApp.openById("1P6gj2UwfRmSMYZ4LBovvqbqy88aluOysPqWWhWbw_VE")
var source_sheet = ss.getSheetByName("Submit");
var destination_sheet = destination.getSheetByName("Point Tracker");
var source_range = source_sheet.getRange("A2:C2");
var editedcell = source_sheet.getRange("C2");
var cell=e.range.getCell(1,1).getA1Notation();
if(cell=='C2' && e.value != 0){
var last_row = destination_sheet.getLastRow();
destination_sheet.insertRowAfter(last_row);
var destination_range = destination_sheet.getRange("A"+(last_row+1)+":C"+(last_row+1));
source_range.copyTo(destination_range,{contentsOnly:true});
source_sheet.getRange("C2").setValue("");
}
}
来源:https://stackoverflow.com/questions/44867032/google-sheets-onedit-specific-cell