我有一個發票模板,它是保存在 GOOGLE DRIVE\SYSTEM\ 中的谷歌檔案,名為 (invoice template.gdoc) 我的目標是將此模板復制到 GOOGLE DRIVE\SYSTEM\INVOICE PDF\INVOICE TEMP FOLDER,使用“replaceText " 創建單獨的發票并將創建的發票以 pdf 格式保存在 GOOGLE DRIVE\SYSTEM\INVOICE PDF 中。并且新的 pdf 可以通過 http 腳本下載,以便用戶可以立即打開和列印。
我的檔案夾結構如下:
GOOGLE DRIVE\SYSTEM\INVOICE PDF\INVOICE TEMP FOLDER (ID: TEMPFOLDER, temp invoice google doc保存在這里,函式結束時會立即洗掉)
GOOGLE DRIVE\SYSTEM\INVOICE PDF\(ID:PDF FOLDER,新創建的pdf保存在這里)
GOOGLE DRIVE\SYSTEM\(此處保存的發票模板)
GOOGLE DRIVE\(谷歌驅動器根目錄)
我達到了我的目標。但是,在 GOOGLE DRIVE ROOT 中出現了一個意外的 google doc 副本,名為 (Copy of invoice template.gdoc)。哪一部分出了問題?
//function to create an invoice pdf
function createInvoiceAndPrint(grabConfirmEntryValue, entryNumber, invoiceNumber, invoiceTime) {
const templateInvoice = DriveApp.getFileById("template file id");
const invoiceTempFolder = DriveApp.getFolderById("TEMPFOLDER");
const invoiceFolder = DriveApp.getFolderById("PDF FOLDER");
const tempInvoiceTemplate = templateInvoice.makeCopy(invoiceTempFolder);
const tempInvoiceDoc = DocumentApp.openById(tempInvoiceTemplate.getId());
const body = tempInvoiceDoc.getBody();
body.replaceText("{EntryNo.}", Utilities.formatString('d', invoiceNumber ));
//and a bunch of rewrite content code here...
tempInvoiceDoc.saveAndClose();
const invoiceContentBlob = tempInvoiceTemplate.getAs(MimeType.PDF);
var newlyCreatedPdf = invoiceFolder.createFile(invoiceContentBlob).setName(invoiceNumber ".pdf");
var downloadLink = HtmlService
.createHtmlOutput('<p>Entry ID: ' entryNumber '<br>Invoice No.: ' invoiceNumber '</p> <p>Print Invoice: <a href="' newlyCreatedPdf.getUrl() '" target="_blank">here</a>.</p>')
.setWidth(300)
.setHeight(200);
SpreadsheetApp.getUi().showModalDialog(downloadLink, "Sales Created");
invoiceTempFolder.removeFile(tempInvoiceTemplate);
}
uj5u.com熱心網友回復:
我認為您的問題的原因However, there is an unexpected copy of google doc in the GOOGLE DRIVE ROOT named (Copy of invoice template.gdoc) appears.是由于invoiceTempFolder.removeFile(tempInvoiceTemplate);。所以,請修改如下。
從:
invoiceTempFolder.removeFile(tempInvoiceTemplate);
至:
// invoiceTempFolder.removeFile(tempInvoiceTemplate); // Remove this line.
- 而且,
removeFolder(child)已經被棄用了。請注意這一點。參考
添加:
從您的以下回復中,
我只需要pdf
如果您想洗掉復制的模板檔案,請修改您的顯示腳本如下。
從:
invoiceTempFolder.removeFile(tempInvoiceTemplate);
至:
tempInvoiceTemplate.setTrashed(true);
- 這樣,復制的模板檔案被移動到垃圾箱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516645.html
