我正在嘗試為特定表的每一行生成 PDF...如果您查看 createPDF(),我在處理 startDate 引數時遇到了一些困難:
- 我想將表中的日期與當月的第一天進行比較;
- 如果表中的日期較新,那么我將使用它作為我的startingDate 引數,否則我想使用當月的第一天。
我猜也許我應該宣告 alet startingDate[]并寫入它,但我似乎無法讓它作業。
你們會那么好心來幫助我嗎?謝謝你!^_^
function createBulkPDFs(){
const docFile = DriveApp.getFileById("1CEJjt50pwNQpK_9tefzpNg5fzie8BeswZV3B4cecpPs");
const tempFolder = DriveApp.getFolderById("1keBnLhrMWjGokTOFdi2o25tjkTKjrKbO");
const pdfFolder = DriveApp.getFolderById("18N2DyrBRNggXle9Pfdi3wbn0NTcdgt93");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Récupération du lead")
const data = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,33).getDisplayValues();
const date = new Date();
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1).toLocaleString("fr-FR").slice(0,10);
let errors = [];
data.forEach(row => {
try{
createPDF(row[2],row[3],row[10],row[11],
/* find startingDate
IF row[4] is newer than firstDayOfMonth select row[4] as startingDate
ELSE select firstDayOfMonth as startingDate
*/
row[2] " " row[3],docFile,tempFolder,pdfFolder);
errors.push([""]);
} catch(err){
errors.push(["Failed"]);
}
});
currentSheet.getRange(2,32,currentSheet.getLastRow()-1,1).setValues(errors);
}
function createPDF(firstName,lasttName,formation,client,startingDate,pdfName,docFile,tempFolder,pdfFolder) {
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{PRENOM}", firstName);
body.replaceText("{NOM}", lasttName);
body.replaceText("{FORMATION}", formation);
body.replaceText("{CLIENT}", client);
body.replaceText("{DEBUT}", debut);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFolder.removeFile(tempFile);
}
看看我的桌子:
| 主ID | 任務編號 | 姓 | 名 | 開始日期 | 截止日期 | 評論 | 進步 | 法規 | 網址會話 | 形成 | 客戶 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| b093eed1-d55c-41f1-9999-644b6af411bb | 120239E15 | 美國能源部 | 約翰 | 2022-05-02 0:00:00 | 2023-01-07 12:00:00 | Lorem ipsum | Lorem ipsum | 好的 | Lorem ipsum | BBB | |
| b093eed1-d55c-41f1-9999-644b6af511cc | 120239Z00 | 史密斯 | 安娜 | 2022-06-02 0:00:00 | 2023-08-09 12:00:00 | Lorem ipsum | Lorem ipsum | 待辦的 | Lorem ipsum | CCC |
uj5u.com熱心網友回復:
嘗試這個
我通過洗掉 try - catch 陳述句修改了您的腳本,然后將其替換為 for 回圈,以便您的電子表格中的每一行都將被逐一檢查。此外,我列出了 for 回圈中的重要變數以減少混淆。
至于日期的比較,我使用了“startingDate >= firstDayOfMonth”,因為當startingDate 比firstDayOfMonth 更新時它回傳true。
另一方面,您的 createPDF() 函式作業得很好。我剛剛修改了“debut”,以便它鏈接最近的日期。
function createBulkPDFs(){
const docFile = DriveApp.getFileById("doc file ID");
const tempFolder = DriveApp.getFolderById("temp folder ID");
const pdfFolder = DriveApp.getFolderById("pdf folder ID");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Récupération du lead")
const date = new Date();
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1).toLocaleString("fr-FR").slice(0,10);
for(i=2; i<=currentSheet.getLastRow(); i ) {
var firstName = currentSheet.getRange(i,4,1,1).getValues();
var lasttName = currentSheet.getRange(i,3,1,1).getValues();
var formation = currentSheet.getRange(i,11,1,1).getValues();
var client = currentSheet.getRange(i,12,1,1).getValues();
var startingDate = currentSheet.getRange(i,5,1,1).getValues();
var debut = (startingDate >= firstDayOfMonth) ? startingDate : firstDayOfMonth;
createPDF(firstName, lasttName, formation, client, debut, firstName " " lasttName, docFile,tempFolder,pdfFolder);
}
}
function createPDF(firstName,lasttName,formation,client,startingDate,pdfName,docFile,tempFolder,pdfFolder) {
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{PRENOM}", firstName);
body.replaceText("{NOM}", lasttName);
body.replaceText("{FORMATION}", formation);
body.replaceText("{CLIENT}", client);
body.replaceText("{DEBUT}", startingDate);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFolder.removeFile(tempFile);
}
輸出
至于輸出,我只是假設首次亮相=startingDate。輸出應如下所示:

pdf 檔案夾包含創建的 PDF。由于您沒有提供 doc 檔案的副本,因此我僅制作了一個測驗用例格式:

腳本運行完成后,應將其轉換為以下內容:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487918.html
標籤:javascript 日期 谷歌应用脚本 相比
上一篇:如何使多個依賴下拉輸入更有效?
下一篇:字典上的遞回字串連接
