我目前有一个代码,其功能与此非常相似,但我不确定需要进行哪些小更改才能使其正常工作。现在,下面的代码比较两行唯一 ID,如果 ID 相同,则会将“注释”列中的单元格复制到另一张工作表。
function setComments() {
var ss = SpreadsheetApp.getActive(),
compare1 = "", compare2 = "",
outputSheet = ss.getSheetByName("Sheet2"),
sourceSheet = ss.getSheetByName("Sheet1"),
range1 = outputSheet.getDataRange(),
range2 = sourceSheet.getDataRange(),
lastCol1 = range1.getNumColumns(),
lastCol2 = range2.getNumColumns(),
values1 = range1.getValues(),
values2 = range2.getValues(),
// get the range of the titles
titleSection1 = outputSheet.getRange(1,1,1, lastCol1),
titleSection2 = sourceSheet.getRange(1,1,1, lastCol2),
// get the values from the titles
titles1 = titleSection1.getValues(),
titles2 = titleSection2.getValues(),
// get the column # for "ID" and "comment"
idCol1 = titles1[0].indexOf("ID"),
idCol2 = titles2[0].indexOf("ID"),
commentsCol1 = titles1[0].indexOf("comment"),
commentsCol2 = titles2[0].indexOf("comment");
// get the IDs from range1
for (i = 1; i < values1.length; i++) {
compare1 = values1[i][idCol1];
// get the IDs from range2
for (j = 1; j< values2.length; j++){
compare2 = values2[j][idCol2];
// if same ID, change the values array
if (compare1 == compare2) {
values1[i][commentsCol1] = values2[j][commentsCol2];
}
}
}
// set values based on the values array
range1.setValues(values1);
}
相反,如果对工作表 1 上的任何单元格进行更改,它将根据另一工作表中的唯一 ID 找到相同的单元格并同步更改。我需要做出什么改变才能完成这项工作?
例如,如果我更改工作表 1 的 ID 1 行中的办公室,则工作表 2 中的 ID 1 也会进行相同的更改。 这是我正在使用的示例表:
Sheet 1:
ID Comment Number Office Clinician
1 good 22345 Dallas
2 bad 12345 Denton
3 good 95954 Lubbock
4 bad 20204 FT.W
5 bad 11111 Denton
6 good 02944 Preston
Sheet 2:
ID Comment Number Office Clinician
1 good 22345 Dallas
3 good 95954 Lubbock
5 bad 11111 Denton
最佳答案
工作表 1 上有一个数据集,工作表 1 上有该数据集的子集。关键字段是“ID”。如果工作表 1 上的数据发生更改,并且在工作表 2 上找到与该更改相关的 ID,则您需要更新工作表 2 上的相关数据集。
这个答案的关键方面是:
onEdit(e)
:这是一个 simple Trigger 。e.range
:“范围”是 Event Object 。通过使用属性“e”;可以恢复大量有关更改的信息。此外,事件对象还可用于获取更多信息,例如(在本例中)行、列和工作表名称。filter(String).length
:有时获取最后一行数据是有问题的。答案获取A列中的所有数据,并使用Javascript“array.filter”方法。在这种情况下,只是将值作为字符串进行计数,并且结果值等于数据的最后一行。- “IF”语句评估多个属性:
- 是否在 Sheet1 上进行编辑?
- 编辑是否在标题行和最后一行之间的行上?
- 编辑是否在包含数据的列中?
- 运算符为“&&”,要求每个属性都必须返回 true。
targetdata.map(function(e){return e[0];});
:脚本获取 Sheet2 上的所有数据,但使用 Javascript array.map方法生成仅包含第 1 列(ID 列)中的值的主题。targetCol1.indexOf(sourceID);
:脚本使用Javascript“array.indexOf”;如果找到ID,则返回Sheet2上该行的索引号;如果没有找到该值,则返回“-1”。允许编写逻辑语句,仅当值不为“-1”时才会执行target.getRange(+result+1,1,1,sourceLC).setValues(sourcedatatocopy)
:脚本的最后几行获取 Sheet 1 上编辑行中的值,然后更新 Sheet2 上的值。注意:它会更新匹配 ID 的所有值 - 而不是识别已更改的字段并仅更新该字段。
function onEdit(e) {
// setup spreadsheet and sheet names
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSN = "Sheet1";
var source = ss.getSheetByName(sourceSN);
var targetSN = "Sheet2";
var target = ss.getSheetByName(targetSN);
// get row, column and sheet of the edited sheet
var row = e.range.getRow();
var col = e.range.getColumn();
var editedsheet = e.range.getSheet().getSheetName();
// Logger.log("DEBUG: editedsheet = "+editedsheet)
// get the ID column from Sheet 1
var SourceCol1Vals = ss.getRange("A2:A").getValues();
var SourceLR = SourceCol1Vals.filter(String).length;
// assign value to last column
var sourceLC = 5;
// test for sheet, row range and column range
if (editedsheet == sourceSN && row >=2 && row <=SourceLR && col <=5){
//Logger.log("DEBUG: this is sheet 1 & the right row and the right Col")
// get the data for this row
var sourcedata = source.getRange(row,1,1,5).getValues();
// get the data from target
var targetdata = target.getDataRange().getValues();
// get only the ID column
var targetCol1 = targetdata.map(function(e){return e[0];});
// get the sourse ID
var sourceID = sourcedata[0][0];
// Logger.log("DEBUG: source ID = "+sourceID)
// search for source ID on the Target list
var result = targetCol1.indexOf(sourceID);
// Logger.log("DEBUG: id was found at "+result);
if (result !=-1){
// if -1 then couldn't find the ID, otherwise it returns the index number where it finds the match
// get the data for the Source ID
var sourcedatatocopy = source.getRange(row,1,1,sourceLC).getValues();
// update the darget for the sourceID data.
target.getRange(+result+1,1,1,sourceLC).setValues(sourcedatatocopy)
}
} else{
// Logger.log("DEBUG: not sheet 1 or right row or right col");
}
return;
}
关于javascript - 根据谷歌表格脚本中的唯一ID同步行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60639108/