You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改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);   
}

关键修改说明

  1. 空值判断逻辑:在原有的匹配行判断基础上,新增了对matchRow[3](即原表格第4列的值)的空值校验,只要该值为null、undefined、空白字符串或仅含空格,就返回"Pending"。
  2. 优化写入范围:将原dsh.getRange("C2:C").setValues(matchingID)改为dsh.getRange(2, 3, matchingID.length, 1).setValues(matchingID),避免写入超出实际需要的空白行,提升脚本效率。

内容的提问来源于stack exchange,提问作者HOANG TRUNG LE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 00:30:53