参数无效:使用 google 脚本填充 google 文档时出现替换错误
Invalid argument: replacement error when populating google doc using google script
我编写了一个代码,用于将电子表格中的数据填充到 google 文档中,并使用 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 行时,代码有效。我不明白问题是什么,如果我注释掉这些行是否有效。
在您的脚本中,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]
的值是否是你想要的。如果这些值不是您期望的值,请再次检查您的电子表格。
参考:
我编写了一个代码,用于将电子表格中的数据填充到 google 文档中,并使用 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 行时,代码有效。我不明白问题是什么,如果我注释掉这些行是否有效。
在您的脚本中,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]
的值是否是你想要的。如果这些值不是您期望的值,请再次检查您的电子表格。