[英]AppScript Google Sheets: Match Serial #s from checkout sheet to the main sheet then update cells in main sheet
[英]Google Sheets - Update Sheet 1 with data from Sheet 2 if a value on both sheets match
我在一個電子表格上有兩張紙:
https://docs.google.com/spreadsheets/d/1ahtB9M6rl6aHmj51ZlP_qDqyUwhpsSVg1r16BafcKIo/edit?usp=sharing
如果每個工作表的ID列中的值都匹配,我想使用Google Apps腳本更新“報表”表中C列中的值,並使用“新數據”表中E列中的值。
在生產中,我期望會有多個唯一的匹配項。 我最好怎么做?
作為一個簡單的示例,在下面的模擬表中,我希望報表ID的“狀態”列中的值(行ID為12)從“已排隊”更新為“贏得”。
報告表
ID | 州
12 | 排隊
34 | 丟失
新數據表
ID | 州
56 | 丟失
12 | 韓元
本周有一個類似的問題問。
這是我的建議,盡管您需要調整工作表和標題的名稱!
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.