拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 无效自变量:使用谷歌脚本填充谷歌档案时出现替换错误

无效自变量:使用谷歌脚本填充谷歌档案时出现替换错误

白鹭 - 2022-03-16 2084 0 0

我撰写了一个代码来将电子表格中的资料填充到谷歌档案中,并使用 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 行的其余部分时,代码有效。我无法理解问题是什么,如果我注释掉这些行,它是否有效。

uj5u.com热心网友回复:

在您的脚本中,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]to的值是否row[11]是你想要的。如果这些值不是您期望的值,请再次检查您的电子表格。

参考:

  • 替换文本(搜索模式,替换)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *