我已经尝试调试这段代码几个小时了,但没有成功。我有一个脚本(下面的代码)绑定到一个名为“EDIT”的谷歌工作表。我试图做到这一点,以便当有人在“编辑”表的一部分中输入一个人的名字时,脚本将自动引用(通过 onEdit 触发器)我拥有的联系信息电子表格(单独的电子表格),找到该行与该人的姓名,然后从该行复制他们的电子邮件地址并将其粘贴到该人姓名旁边的我的“编辑”电子表格中。不过,我在调用另一个电子表格时不断遇到问题。有什么建议吗?
代码:
function onEdit(e){
//making sure the edited range is from the correct sheet
if(e.range.getSheet().getName() == "EDIT"){
var eRange = e.range;
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EDIT");
var contactSheet = SpreadsheetApp.openById("1IooSZ8jclhwWTZDKc_h_o4wpQoMyuDHVT5q8CqAbT7M").getSheetByName("2024");
//iterating through each cell of the edited range (for each new name)
for (var n=1;n<=eRange.getNumRows();n++){
var cell = eRange.getCell(n,1)
//making sure cell of editted range is withing range of interest
if(cell.getA1Notation() == "J25"||cell.getA1Notation() == "J26"||cell.getA1Notation() == "J27"||cell.getA1Notation() == "J28"||cell.getA1Notation() == "J29"||cell.getA1Notation() == "J30"||cell.getA1Notation() == "J31"||cell.getA1Notation() == "J32"||cell.getA1Notation() == "J33"||cell.getA1Notation() == "J34"||cell.getA1Notation() == "J35"||cell.getA1Notation() == "J36"||cell.getA1Notation() == "J37"){
//making sure cell has name, wasn't just edited to be blank (deleted text)
if(cell.getValue() !== ""){
var nameToFind = cell.getValue();
var firstAndLastNames = contactSheet.getRange(2,3,contactSheet.getLastRow(),2).getValues();
var fullNames = [];
for (i = 0;i<firstAndLastNames.length;i++){
fullNames[i] = (firstAndLastNames[i][0] + " " + firstAndLastNames[i][1])
if (fullNames[i] == nameToFind){
var email = contactSheet.getRange(i+2,10).getValue();
ss.getRange(eRange.getRow(),13).setValue(email);
}
SpreadsheetApp.flush()
}
}
}}
}
}
没有代表发表评论,但首先要注意的是,分享您收到的具体错误消息将会有所帮助。话虽如此,我建议对下面的代码进行一些更改(包括将对外部表的调用放在 try/catch 块中,以确保捕获那里的任何错误)
function onEdit(e){
//making sure the edited range is from the correct sheet
if(e.range.getSheet().getName() == "EDIT"){
var eRange = e.range;
var eValues = e.range.getValues();//we'll use this later to update the spreadsheet in one go...
var updatedValues = false //changes to true below, if a change is made...
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EDIT"); //don't think you need this
//just get the data from this sheet once, rather than every loop... Also put in a try/catch loop to catch any errors you might be getting here
try {
var contactSheet = SpreadsheetApp.openById("1IooSZ8jclhwWTZDKc_h_o4wpQoMyuDHVT5q8CqAbT7M").getSheetByName("2024");
var firstAndLastNames = contactSheet.getRange(2,3,contactSheet.getLastRow(),2).getValues();
} catch(e) {
Logger.log("Error getting contact sheet: %s", e.message)
return; //no point continuing if you get an error here
}
//iterating through each cell of the edited range (for each new name)
for (var n=1;n<=eRange.getNumRows();n++){
var cell = eRange.getCell(n,1)
const interestRange = ["J25", "J26", "J27", "J28", "J29"]; //you get the gist
//easier to read if statement follows...
if (interestRange.includes(cell.getA1Notation())) {
//making sure cell has name, wasn't just edited to be blank (deleted text)
if(cell.getValue() !== ""){
var nameToFind = cell.getValue();
// moved this line below outside the loop...
//var firstAndLastNames = contactSheet.getRange(2,3,contactSheet.getLastRow(),2).getValues();
var fullNames = [];
for (i = 0;i<firstAndLastNames.length;i++){
fullNames[i] = (firstAndLastNames[i][0] + " " + firstAndLastNames[i][1])
if (fullNames[i] == nameToFind) {
var email = contactSheet.getRange(i+2,10).getValue();
// ss.getRange(eRange.getRow(),13).setValue(email)
//try this instead, using 12 as the values array starts at zero
eValues[i][12] = email;
updatedValues = true; //you made a change to the values, so signal it here.
}
//don't need this - see below
//SpreadsheetApp.flush()//
}
}
}
}
//Now outside the loop - only need to update the spreadsheet IF values were changed...
if (updatedValues) {
try {
eRange.setValue(eValues) // this will update the entire range.
} catch(e) {
Logger.log("Spreadsheet update failed: %s", e.message)
}
}
}
}