【问题标题】:Invalid argument: replacement error when populating google doc using google script无效参数:使用谷歌脚本填充谷歌文档时出现替换错误
【发布时间】:2021-12-21 09:59:54
【问题描述】:

我编写了一个代码,将电子表格中的数据填充到谷歌文档中,并使用 g-sript 将其保存到驱动器。这是相同的代码:

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('Invoice creator');
  menu.addItem('Generate Invoice', 'invoiceGeneratorFunction');
  menu.addToUi();
}


function invoiceGeneratorFunction() {
  const invoiceTemplate = DriveApp.getFileById('125NPu-n77F6N8hez9w63oSzbWrtryYpRGOkKL3IbxZ8');
  const destinationFolder = DriveApp.getFolderById('163_wLsNGkX4XDUiSOcQ88YOPe3vEx7ML');
  const sheet_invoice = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('New Invoice Sheet');

  const rows = sheet_invoice.getDataRange().getValues();
  Logger.log(rows);

  rows.forEach(function(row, index) {
    if (index === 0) return;
    if (row[12] != "") return;

    const copy = invoiceTemplate.makeCopy(`${row[1]} VIN Number: ${row[2]}`,destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();

    var friendlyDateBilled = new Date(row[0]).toLocaleDateString();
    var friendlyDateDelivery = new Date(row[3]).toLocaleDateString();

    body.replaceText('{{Date Billed}}',friendlyDateBilled);
    body.replaceText('{{Customer Name}}',row[1]);
    body.replaceText('{{VIN Number}}',row[2]);
    body.replaceText('{{Date of Delivery}}',friendlyDateDelivery);
    body.replaceText('{{Package}}',rows[4]);
    body.replaceText('{{Price}}',rows[5]);
    body.replaceText('{{Output CGST}}',rows[6]);
    body.replaceText('{{Output SGST}}',rows[7]);
    body.replaceText('{{Discount}}',rows[8]);
    body.replaceText('{{Total Price}}',rows[9]);
    body.replaceText('{{Balance}}',rows[10]);
    body.replaceText('{{Remarks}}',rows[11]);

    doc.saveAndClose();

    const url = doc.getUrl();
    sheet_invoice.getRange(index+1, 13).setValue(url);

  })

}

我已经为脚本创建了一个菜单按钮来运行。但是当我运行它时,我收到一条错误消息:

异常:无效参数:替换 在未知功能 在 invoiceGeneratorFunction(代码:17:8)

(这里第 32 行是 body.replaceText('{{Package}}',rows[4]); 第 17 行是 forEach 的开始)

有趣的是,当我注释掉该行之后的其余 body.replaceText 行时,代码可以正常工作。如果我注释掉这些行,我无法理解问题是什么。

【问题讨论】:

    标签: javascript google-apps-script google-docs


    【解决方案1】:

    在您的脚本中,rows 是使用sheet_invoice.getDataRange().getValues() 检索的二维数组。当我看到你的循环时,在body.replaceText('{{Package}}',rows[4]); 之后,使用了rows。在这种情况下,rows[4] 是一维数组。它必须是replaceText(searchPattern, replacement) 的参数的字符串。我认为这可能是您的问题的原因。为了消除这个问题,下面的修改如何?

    发件人:

    body.replaceText('{{Package}}',rows[4]);
    body.replaceText('{{Price}}',rows[5]);
    body.replaceText('{{Output CGST}}',rows[6]);
    body.replaceText('{{Output SGST}}',rows[7]);
    body.replaceText('{{Discount}}',rows[8]);
    body.replaceText('{{Total Price}}',rows[9]);
    body.replaceText('{{Balance}}',rows[10]);
    body.replaceText('{{Remarks}}',rows[11]);
    

    收件人:

    body.replaceText('{{Package}}',row[4]);
    body.replaceText('{{Price}}',row[5]);
    body.replaceText('{{Output CGST}}',row[6]);
    body.replaceText('{{Output SGST}}',row[7]);
    body.replaceText('{{Discount}}',row[8]);
    body.replaceText('{{Total Price}}',row[9]);
    body.replaceText('{{Balance}}',row[10]);
    body.replaceText('{{Remarks}}',row[11]);
    

    注意:

    • 我不确定rows 的实际值。所以我不确定row[4]row[11] 的值是否是你想要的。如果这些值不是您期望的值,请再次检查您的电子表格。

    参考:

    【讨论】:

    • 这解决了它......这是一个愚蠢的错误......非常感谢!
    • @Lakshya Bhadoo 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多