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

如何在Google Sheets中复制带@提及的单元格至另一单元格

解决Google表格脚本复制@提及格式的问题

原脚本使用getValue()只能提取单元格的纯文本内容,无法保留@提及这类智能芯片的格式,同时脚本存在变量未定义、重复声明的错误,以下是修正后的可行方案:

方法1:直接复制完整单元格内容(推荐)

使用copyTo方法可以直接复制单元格的所有内容、格式及智能芯片,操作最简便:

function syncMentions() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet1 = ss.getSheetByName("BDCP");
  const sheet2 = ss.getSheetByName("Campaigns");

  const bdValues = sheet1.getRange("B2:C50").getValues();

  for (let rowIndex = 0; rowIndex < bdValues.length; rowIndex++) {
    if (bdValues[rowIndex][0] === "ACTIVE") {
      // 计算源单元格行号(从B2开始,索引+2)
      const sourceRow = rowIndex + 2;
      // 源单元格(C列对应行)
      const sourceCell = sheet1.getRange(`C${sourceRow}`);
      // 目标单元格(从B15开始,对应偏移索引行)
      const targetCell = sheet2.getRange(`B${15 + rowIndex}`);
      // 复制所有内容和格式,包括@提及智能芯片
      sourceCell.copyTo(targetCell, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    }
  }
}

方法2:通过富文本对象精准控制

如果需要更精细的内容控制,可以获取单元格的富文本值后再设置到目标单元格:

function syncMentionsRichText() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet1 = ss.getSheetByName("BDCP");
  const sheet2 = ss.getSheetByName("Campaigns");

  const bdValues = sheet1.getRange("B2:C50").getValues();

  for (let rowIndex = 0; rowIndex < bdValues.length; rowIndex++) {
    if (bdValues[rowIndex][0] === "ACTIVE") {
      const sourceRow = rowIndex + 2;
      // 获取包含@提及格式的富文本对象
      const richText = sheet1.getRange(`C${sourceRow}`).getRichTextValue();
      const targetRow = 15 + rowIndex;
      // 将富文本对象设置到目标单元格
      sheet2.getRange(targetRow, 2).setRichTextValue(richText);
    }
  }
}

关键修正点

  • 修复原脚本中未定义的line变量,改为基于循环索引计算行号
  • 移除for循环内重复声明的row变量,避免语法冲突
  • 替换getValue()/setValue()为能保留格式的方法,确保@提及智能芯片被完整复制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 08:05:24