我在一个电子表格中有两张纸:
https://docs.google.com/spreadsheets/d/1ahtB9M6rl6aHmj51ZlP_qDqyUwhpsSVg1r16BafcKIo/edit?usp=sharing
使用Google Apps脚本,我想在“报告”表格中更新C列中的值,并在“新数据”表格中使用E列中的值,如果每张表格上的ID列中的值匹配。
在制作中,我期待有多个独特的比赛。我最好怎么做?
作为一个简单的例子,在下面的模拟表中,我想要报告表的“STATE”列中的值,其中行ID为12,从“已排队”更新为“赢”。
报告表
ID | STATE
12 |排队
34 |丢失
新数据表
ID | STATE
56 |丢失
12 |韩元
答案 0 :(得分:0)
本周有人问similar question。
这是我的建议,尽管你需要调整表格和标题的名称!
function setCommon() {
var ss = SpreadsheetApp.getActive(),
compare1 = "", compare2 = "",
outputSheet = ss.getSheetByName("Sheet1"),
sourceSheet = ss.getSheetByName("Sheet2"),
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);
}