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

Google Sheets公式生成二维码插入Google Docs异常问题求助

修复Google Sheets二维码插入Google Docs后跳转错误问题

我用以下代码尝试将Google Sheets「QR CODE GENERATOR」工作表内公式生成的二维码插入指定Google Docs:

//worksheets
const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("QR CODE GENERATOR");
//lastrow
const lastrow_ws = ws.getLastRow();

function qrCode(){
  var documentID = ws.getRange(lastrow_ws, 10).getValue();
  Logger.log(documentID)
  var doc = DocumentApp.openById(documentID)
  var qrCode = ws.getRange(lastrow_ws, 2).getValue(); // Get the QR code value from the Spreadsheet
  var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=&" + qrCode 
  var resp = UrlFetchApp.fetch(url); // Get the image of QR code
  var barcode = doc.getChild(24).asParagraph().appendInlineImage(resp.getBlob()); // Value of child depend of where you want your QR code.
}

目前代码能插入二维码,但扫描后会跳转到Google搜索「CellImage」的页面,和工作表里的对应二维码不符。

问题原因

  1. getValue()获取的是单元格图片的占位值(比如CellImage),并非二维码对应的实际目标内容;
  2. 构造Google Charts URL时多了一个多余的&,导致chl参数传递错误。

修复方案

根据工作表二维码的生成方式,提供两种修复代码:

方案1:从公式提取目标内容重新生成二维码

如果工作表用=IMAGE("https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=目标链接")这类公式生成二维码,用以下代码提取目标内容并生成正确的二维码:

const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("QR CODE GENERATOR");
const lastrow_ws = ws.getLastRow();

function qrCode(){
  const documentID = ws.getRange(lastrow_ws, 10).getValue();
  const doc = DocumentApp.openById(documentID);
  
  // 获取单元格的公式内容
  const cellFormula = ws.getRange(lastrow_ws, 2).getFormula();
  // 从公式中提取二维码的目标链接
  const chlMatch = cellFormula.match(/chl=([^"]+)/);
  if (!chlMatch) {
    throw new Error("无法从单元格公式中提取二维码目标内容");
  }
  const qrContent = chlMatch[1];
  
  // 正确构造二维码URL,对内容编码避免特殊字符问题
  const url = `https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=${encodeURIComponent(qrContent)}`;
  const resp = UrlFetchApp.fetch(url);
  
  // 插入到文档指定位置(getChild(24)需根据实际文档结构调整)
  doc.getChild(24).asParagraph().appendInlineImage(resp.getBlob());
}

方案2:直接复用工作表的二维码图片URL

如果想直接使用工作表里已经生成好的二维码图片,可提取公式中的完整图片URL:

const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("QR CODE GENERATOR");
const lastrow_ws = ws.getLastRow();

function qrCode(){
  const documentID = ws.getRange(lastrow_ws, 10).getValue();
  const doc = DocumentApp.openById(documentID);
  
  // 获取单元格的公式内容
  const cellFormula = ws.getRange(lastrow_ws, 2).getFormula();
  // 从IMAGE公式中提取完整的二维码图片URL
  const urlMatch = cellFormula.match(/IMAGE\("([^"]+)"\)/);
  if (!urlMatch) {
    throw new Error("无法从单元格公式中提取二维码图片URL");
  }
  const qrImageUrl = urlMatch[1];
  
  const resp = UrlFetchApp.fetch(qrImageUrl);
  doc.getChild(24).asParagraph().appendInlineImage(resp.getBlob());
}

关键改动说明

  • 改用getFormula()获取单元格的公式内容,替代getValue()(后者无法获取图片对应的实际内容);
  • 用正则表达式从公式中提取所需的目标链接或图片URL;
  • 对二维码内容进行encodeURIComponent()编码,避免特殊字符导致URL失效;
  • 修正了原URL中多余的&,确保参数传递正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 21:33:25