我在 Google 表格中制作每日工作日志报告,并且必须每月提交该报告。为此,我的 Google 云端硬盘帐户中有一个模板报告。该模板需要使用 Google 表格中带有占位符 {{}} 的文本和图表进行更新。我的代码将文本占位符替换为适当的文本,但图像占位符(图表:线/饼图/条形图)不会替换为图像。
请帮忙。
这是我的代码:
function docTemplate(){
timeZone = Session.getScriptTimeZone();
const now = Utilities.formatDate(new Date(),timeZone,"MMMM-YYYY")
const googledocTemplate = DriveApp.getFileById('1DXzyPGhjOVKMmb09J7ZCKI-ij7c-QecaPI-jFqe3gEQ');
const destinationFolder = DriveApp.getFolderById('17tWJSKjOdEAMGmTf1l8dKZg52uGpQdNe');
const sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
const dataSheet = getSheetByID('552896708');
const rows = dataSheet.getDataRange().getValues().filter(arr => arr.some(v => !!v));
const chartSheet = getSheetByID('339388881');
const copy = googledocTemplate.makeCopy('Monthly Report:' + now, destinationFolder);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
body.replaceText('{{Date}}',now);
body.replaceText('{{Link}}', sheetURL);
var content = body.getText();
var placeholderPattern = /{{(.*?)}}/g;
var placeholder = content.match(placeholderPattern);
placeholder.forEach(function(placeholder){
var searchText = placeholder.substring(2, placeholder.length-2).trim();
var charts = chartSheet.getCharts()[searchText];
if(charts){
body.replaceText(placeholder, ' ');
body.appendImage(charts);
}
})
doc.saveAndClose();
}
但我无法用模板文档中的图像替换占位符
{{Figure 1}}
和 {{Figure 2}}
。请看下图来理解我的意思。
var searchText = placeholder.substring(2, placeholder.length-2).trim();
是字符串值。但是, chartSheet.getCharts()
是一个数组。因此,chartSheet.getCharts()[searchText];
始终为假。另外,为了放置图表,需要使用 blob。当这些点都体现在你的放映脚本中时,下面的修改如何?
在使用此脚本之前,请根据您的实际情况修改
obj
。在此修改中,为了识别每个图表,我使用了一个包含图表标题和占位符的对象。所以,请根据您的实际情况进行修改。
function docTemplate() {
timeZone = Session.getScriptTimeZone();
const now = Utilities.formatDate(new Date(), timeZone, "MMMM-YYYY")
const googledocTemplate = DriveApp.getFileById('1DXzyPGhjOVKMmb09J7ZCKI-ij7c-QecaPI-jFqe3gEQ');
const destinationFolder = DriveApp.getFolderById('17tWJSKjOdEAMGmTf1l8dKZg52uGpQdNe');
const sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
const dataSheet = getSheetByID('552896708');
const rows = dataSheet.getDataRange().getValues().filter(arr => arr.some(v => !!v));
const chartSheet = getSheetByID('339388881');
const copy = googledocTemplate.makeCopy('Monthly Report:' + now, destinationFolder);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
body.replaceText('{{Date}}', now);
body.replaceText('{{Link}}', sheetURL);
var content = body.getText();
var placeholderPattern = /{{(.*?)}}/g;
var placeholder = content.match(placeholderPattern);
// --- I modified the below script.
// Ref: https://tanaikech.github.io/2018/08/20/replacing-text-to-image-for-google-document-using-google-apps-script/ Author: me
// Ref: https://stackoverflow.com/q/51912364
var replaceTextToImage = function (body, searchText, image, width) {
var next = body.findText(searchText);
if (!next) return;
var 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);
}
return next;
};
var obj = [
{ chartTitle: "sample1", placeholder: "{{Figure 1}}" },
{ chartTitle: "sample2", placeholder: "{{Figure 2}}" },
];
chartSheet.getCharts().forEach(c => {
var title = c.getOptions().get("title");
var p = obj.find(({ chartTitle }) => chartTitle == title);
if (p) {
replaceTextToImage(body, p.placeholder, c.getBlob());
}
});
// ---
doc.saveAndClose();
}
replaceTextToImage(body, p.placeholder, c.getBlob());
修改为 `replaceTextToImage(body, p.placeholder, c.getBlob(), 200);。obj
以及电子表格和文档运行此脚本时,我确认 {{Figure 1}}
和 {{Figure 2}}
的占位符已正确替换为所需的图表。