我撰寫了一個代碼來將電子表格中的資料填充到谷歌檔案中,并使用 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]是你想要的。如果這些值不是您期望的值,請再次檢查您的電子表格。
參考:
- 替換文本(搜索模式,替換)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389694.html
