修改AppScript实现Vlookup:匹配值为空时填入'Pending'
修改后的跨表格Vlookup脚本
以下是调整后的脚本,当匹配到的值为空(包括空白字符串、仅含空格的情况)时,会自动写入"Pending";未找到匹配项时仍保留原提示"Không tìm thấy dữ liệu":
function getTracking() { const sss = SpreadsheetApp.openById("1B2yhG1dhpqgtSfF4ngqRuH51kzwQ6VwxIvCmwZZ2yQU"); const ssh = sss.getSheetByName("Tổng Hợp"); const mDB = ssh.getRange(2,1,ssh.getLastRow()-1,4).getValues(); const dss = SpreadsheetApp.openById("1slt0ExJK2X8xkRVpOzdD8NCsUmj3UeC3urfyZWsV0To"); const dsh = dss.getSheetByName("test"); const searchValues = dsh.getRange("B2:B").getValues(); const matchingID = searchValues.map(searchRow => { const matchRow = mDB.find(r => r[0] == searchRow[0]); // 新增空值判断:匹配到行但对应值为空时返回"Pending" return matchRow ? (!matchRow[3] || matchRow[3].toString().trim() === "" ? ["Pending"] : [matchRow[3]]) : ["Không tìm thấy dữ liệu"]; }) dsh.getRange(2, 3, matchingID.length, 1).setValues(matchingID); }
关键修改说明
- 空值判断逻辑:在原有的匹配行判断基础上,新增了对
matchRow[3](即原表格第4列的值)的空值校验,只要该值为null、undefined、空白字符串或仅含空格,就返回"Pending"。 - 优化写入范围:将原
dsh.getRange("C2:C").setValues(matchingID)改为dsh.getRange(2, 3, matchingID.length, 1).setValues(matchingID),避免写入超出实际需要的空白行,提升脚本效率。
内容的提问来源于stack exchange,提问作者HOANG TRUNG LE
相关产品推荐
相关产品推荐

