我有一個基于 Apps Script 的訂閱表格,它允許我收集訂閱者的個人資料和簽名。當用戶提交表單時,資料被發送到電子表格,簽名存盤在特定檔案夾中,并以提交 ID 命名,該 ID 與在 Google 表格中匯入的資料的 ID 相同。
我需要使用從 Google 表格電子表格中提取的資料和來自 Google Drive 的特定名稱影像自動填充 Google Docs 檔案。然后,將 Google Docs 檔案轉換為 PDF。
這是電子表格的螢屏截圖。
這是簽名檔案夾的截圖。
這是Google Docs 模板檔案的螢屏截圖。
以下是我用來從 Google Docs 檔案生成 PDF 的代碼,該檔案填充了從 Google Sheets 電子表格中提取的資料。
function createBulkPDFs() {
const docFile = DriveApp.getFileById("1ZxvyViklifScfOt90YX4R-NgOTbO044rUSeyQfVCzds");
const tempFolder = DriveApp.getFolderById("1yFLVyTtb6tW-UGdf1hkKHdzGIKG7qg2W");
const pdfFolder = DriveApp.getFolderById("1YGG1Y_u2jOMbfSIh3Ajb4kKGU-tg2oWK");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Responses");
const data = currentSheet.getRange(2, 1,currentSheet.getLastRow()-1,15).getDisplayValues();
let errors = [];
data.forEach(row => {
try{
createPDF (row[2],row[3],row[7],row[8],row[4],row[9],row[12],row[10],row[11],row[5],row[6],row[13],row[1],row[0],row[2],docFile,tempFolder,pdfFolder);
errors.push(["Completata"]);
} catch(err){
errors.push(["Fallita"]);
}
});
currentSheet.getRange(2, 16,currentSheet.getLastRow()-1,1).setValues(errors);
}
function createPDF (name,surname,placebirth,daybirth,gender,street,zipcode,city,province,email,phone,instagram,id,timestamp,pdfName,docFile,tempFolder,pdfFolder) {
// ID Template Modulo di Iscrizione: 1ZxvyViklifScfOt90YX4R-NgOTbO044rUSeyQfVCzds
// ID tempFolder: 1yFLVyTtb6tW-UGdf1hkKHdzGIKG7qg2W
// ID pdfFolder: 1YGG1Y_u2jOMbfSIh3Ajb4kKGU-tg2oWK
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{name}", name);
body.replaceText("{surname}", surname);
body.replaceText("{placebirth}", placebirth);
body.replaceText("{daybirth}", daybirth);
body.replaceText("{gender}", gender);
body.replaceText("{street}", street);
body.replaceText("{zipcode}", zipcode);
body.replaceText("{city}", city);
body.replaceText("{province}", province);
body.replaceText("{email}", email);
body.replaceText("{phone}", phone);
body.replaceText("{instagram}", instagram);
body.replaceText("{timestamp}", timestamp);
body.replaceText("{id}", id);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFile.setTrashed(true);
}
現在我還需要檢索簽名影像檔案并將其放入 Google Docs 檔案中。
這個想法是腳本應該搜索以特定 ID 命名的影像,并將其放在 Google Docs 檔案中,替換文本占位符 {signature}。
有沒有辦法做到這一點?
非常感謝!
編輯:
在執行 Tanaike 的代碼建議后,腳本現在成功地編輯了 Doc 檔案并用資料和簽名影像填充它。
盡管如此,它并沒有將檔案轉換為應有的 PDF。
這是當前狀態的代碼。
function createBulkPDFs() {
const docFile = DriveApp.getFileById("1ZxvyViklifScfOt90YX4R-NgOTbO044rUSeyQfVCzds");
const tempFolder = DriveApp.getFolderById("1yFLVyTtb6tW-UGdf1hkKHdzGIKG7qg2W");
const pdfFolder = DriveApp.getFolderById("1YGG1Y_u2jOMbfSIh3Ajb4kKGU-tg2oWK");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Responses");
const data = currentSheet.getRange(2, 1,currentSheet.getLastRow()-1,15).getDisplayValues();
let errors = [];
data.forEach(row => {
try{
createPDF (row[2],row[3],row[7],row[8],row[4],row[9],row[12],row[10],row[11],row[5],row[6],row[13],row[1],row[0],row[0] " " row[2] " " row[3],docFile,tempFolder,pdfFolder);
errors.push(["Completata"]);
} catch(err){
errors.push(["Fallita"]);
}
});
currentSheet.getRange(2, 16,currentSheet.getLastRow()-1,1).setValues(errors);
}
function createPDF (name,surname,placebirth,daybirth,gender,street,zipcode,city,province,email,phone,instagram,id,timestamp,pdfName,docFile,tempFolder,pdfFolder) {
// ID Template Modulo di Iscrizione: 1ZxvyViklifScfOt90YX4R-NgOTbO044rUSeyQfVCzds
// ID tempFolder: 1yFLVyTtb6tW-UGdf1hkKHdzGIKG7qg2W
// ID pdfFolder: 1YGG1Y_u2jOMbfSIh3Ajb4kKGU-tg2oWK
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{name}", name);
body.replaceText("{surname}", surname);
body.replaceText("{placebirth}", placebirth);
body.replaceText("{daybirth}", daybirth);
body.replaceText("{gender}", gender);
body.replaceText("{street}", street);
body.replaceText("{zipcode}", zipcode);
body.replaceText("{city}", city);
body.replaceText("{province}", province);
body.replaceText("{email}", email);
body.replaceText("{phone}", phone);
body.replaceText("{instagram}", instagram);
body.replaceText("{timestamp}", timestamp);
body.replaceText("{id}", id);
const imageFolderId = "1mdJbbG_0aF8wjEIuVPsMr9jV31wPINRk"; // Please set the folder ID of your "signatureFolder".
const filename = id; // or id
const files = DriveApp.getFolderById(imageFolderId).getFilesByName(filename);
let image;
if (files.hasNext()) {
image = files.next().getBlob();
} else {
throw new Error(`No file of "${filename}"`);
}
do {
const width = 200;
const next = body.findText("{signature}");
if (!next) break;
const r = next.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
if (width && typeof width == "number") {
img.setWidth(width);
img.setHeight(width * img.getHeight() / img.getWidth());
}
} while (next);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFile.setTrashed(true);
}
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想用
{sigunature}從 Google 檔案中的“signatureFolder”檔案夾中檢索到的影像替換 的文本。
當你的腳本被修改時,下面的修改呢?在這種情況下,請createPDF按如下方式修改功能。
從:
body.replaceText("{id}", id);
tempDocFile.saveAndClose();
至:
body.replaceText("{id}", id);
// --- I added below script.
const imageFolderId = "###"; // Please set the folder ID of your "signatureFolder".
const filename = id; // or `${id}.png`;
const files = DriveApp.getFolderById(imageFolderId).getFilesByName(filename);
let image;
if (files.hasNext()) {
image = files.next().getBlob();
} else {
throw new Error(`No file of "${filename}"`);
}
let next;
do {
const width = 200;
next = body.findText("{signature}");
if (!next) break;
const r = next.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
if (width && typeof width == "number") {
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);
}
} while (next);
// ---
tempDocFile.saveAndClose();
筆記:
在使用此腳本之前,請先設定
imageFolderId. 這是您的“signatureFolder”的檔案夾 ID。另外,請檢查每個影像的檔案名。不幸的是,我不知道您的實際檔案名。所以,從你的展示圖片來看,我猜你的檔案名可能是
id1.pngorid1。所以,請const filename = `${id}.png`; // or id根據您的實際情況進行修改。- 如果檔案名是
id1.png,請使用const filename = `${id}.png`;. - 如果檔案名是
id1,請使用const filename = id;.
- 如果檔案名是
如果出現類似錯誤
No file of ###,請再次檢查檔案名。并且,
const width = 200;是插入影像的影像大小。請根據您的實際情況進行修改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517658.html
