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

Google Script:如何通过Issue Key匹配导入数据行并更新变更字段

解决Google Sheets中Jira数据匹配更新问题(保留手动列)

以下是可直接运行的Google Apps Script代码,专门处理Jira数据的匹配更新,完全保留手动填写列的内容:

function updateJiraData() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const targetSheet = ss.getSheetByName("主数据表格"); // 替换为你的主表名称
  const importRange = "A:G"; // 7个导入列的范围,按实际列调整(如B:H)
  const manualColumns = ["H", "I", "J", "K", "L"]; // 5个手动列的列标,按实际调整

  // 读取主表和临时导入表数据(临时表存放每次新导入的JQL数据)
  const tempSheet = ss.getSheetByName("JQL临时导入表"); // 替换为你的临时表名称
  const targetData = targetSheet.getDataRange().getValues();
  const tempData = tempSheet.getDataRange().getValues();
  if (targetData.length === 0 || tempData.length === 0) return;

  // 构建Issue Key到主表行索引的映射(默认Issue Key在导入列第一列)
  const issueKeyMap = new Map();
  const issueKeyColIndex = targetSheet.getRange(importRange).getColumn() - 1;
  for (let i = 1; i < targetData.length; i++) {
    const issueKey = targetData[i][issueKeyColIndex];
    if (issueKey) issueKeyMap.set(issueKey, i);
  }

  // 整理导入列的数组索引
  const importStartCol = targetSheet.getRange(importRange).getColumn();
  const importEndCol = targetSheet.getRange(importRange).getLastColumn();
  const importColIndices = [];
  for (let col = importStartCol; col <= importEndCol; col++) {
    importColIndices.push(col - 1);
  }

  // 遍历新数据,执行更新或新增操作
  for (let i = 1; i < tempData.length; i++) {
    const issueKey = tempData[i][issueKeyColIndex];
    if (!issueKey) continue;

    if (issueKeyMap.has(issueKey)) {
      // 匹配到现有Issue Key,仅更新导入列
      const targetRowNum = issueKeyMap.get(issueKey) + 1;
      const targetRange = targetSheet.getRange(targetRowNum, importStartCol, 1, importEndCol - importStartCol + 1);
      const newImportValues = tempData[i].filter((_, idx) => importColIndices.includes(idx));
      targetRange.setValues([newImportValues]);
    } else {
      // 无匹配Issue Key,新增行并保留手动列空值
      const newRow = tempData[i].slice();
      manualColumns.forEach(col => {
        const colIndex = targetSheet.getRange(col + 1).getColumn() - 1;
        newRow[colIndex] = "";
      });
      targetSheet.appendRow(newRow);
    }
  }

  // 清空临时表(可选,根据需求保留)
  tempSheet.clearContents();
}

关键配置与说明

  • 表格名称替换:将代码中的主数据表格和JQL临时导入表改为你实际使用的表格名称,临时表专门用来存放每次新导入的JQL数据
  • 列范围调整:
    • importRange:设置7个Jira导入列的范围(如导入列是C到I则写"C:I")
    • manualColumns:设置5个手动填写列的列标(如手动列是M到Q则写["M", "N", "O", "P", "Q"])
  • Issue Key位置:默认Issue Key在导入列的第一列,若你的Issue Key在导入列的其他位置,需调整issueKeyColIndex的计算逻辑
  • 更新逻辑:仅更新Jira导入列的内容,手动填写列的任何数据都不会被修改;未匹配到的Issue Key会自动新增一行

使用步骤

  1. 在Google表格中新建临时表,每次先将JQL数据导入到该临时表
  2. 打开「扩展程序」→「Apps脚本」,粘贴上述代码并保存
  3. 首次运行需完成授权验证,测试脚本功能是否正常
  4. 设置定时触发器:在脚本编辑器中点击「编辑」→「当前项目的触发器」,添加时间驱动触发器,设置每日两次自动运行updateJiraData函数

内容的提问来源于stack exchange,提问作者GingerBear

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 23:43:17