我正在尝试编辑此“从模板生成”脚本。该脚本所做的是基于一个模板,它将信息从电子表格输入到文档。我喜欢它的工作方式,只是它只是将文档命名为模板的副本,然后是行号。这对我试图使用它的效率不是很高。我的问题是我如何能够做到这些选择之一:
答:名称必须基于行中的某个单元格。例如,有一个名为Claim#和Department的列。我希望这两个文件能够构成每个文档的标题。该文档将是:“{department name} {claim#}”,基于该行中导出的该列中的信息。
B:在生成询问我想要命名文件的文档时,请放入一个框。
感谢您的帮助!
PS:如果需要,我将用于文档名称的列是:E列,然后是D列。
这是代码:(我没有做到这一点,我在画廊中找到了它。)
function generateDocument(e) {
var template = DocsList.getFileById(e.parameter.Templates);
var row = e.parameter.row
var myDocID = template.makeCopy(template.getName()+" - "+ row).getId();
var myDoc = DocumentApp.openById(myDocID);
var copyBody = myDoc.getActiveSection();
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
//Browser.msgBox(row);
var myRow = SpreadsheetApp.getActiveSpreadsheet().getRange(row+":"+row);
for (var i=1;i<Sheet.getLastColumn()+1;i++){
var myCell = myRow.getCell(1, i);
copyBody.replaceText("{"+myCell.getA1Notation().replace(row,"")+"}", myCell.getValue());
}
myDoc.saveAndClose();
//var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
//MailApp.sendEmail(email_address, subject, body, {cc: carbonCopy, name: senderName, htmlBody: body, attachments: pdf});
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function getTemplates() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Generate from template');
// Create a grid with 3 text boxes and corresponding labels
var grid = app.createGrid(3, 2);
grid.setWidget(0, 0, app.createLabel('Template name:'));
var list = app.createListBox();
list.setName('Templates');
grid.setWidget(0, 1, list);
var docs = DocsList.getFolder("Templates").getFilesByType("document");
for (var i = 0; i < docs.length; i++) {
list.addItem(docs[i].getName(),docs[i].getId());
}
grid.setWidget(1, 0, app.createLabel('Row:'));
var row = app.createTextBox().setName('row');
row.setValue(SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getRow());
grid.setWidget(1, 1, row);
// Create a vertical panel..
var panel = app.createVerticalPanel();
// ...and add the grid to the panel
panel.add(grid);
// Create a button and click handler; pass in the grid object as a callback element and the handler as a click handler
// Identify the function b as the server click handler
var button = app.createButton('Submit');
var handler = app.createServerClickHandler('generateDocument');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the panel and the panel to the application, then display the application app in the Spreadsheet doc
panel.add(button);
app.add(panel);
doc.show(app);
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{name: "Generate from template", functionName: "getTemplates"}];
ss.addMenu("Template Generator", menuEntries);
}
答案 0 :(得分:0)
makeCopy方法包括为副本命名的可能性,自动填充显示清楚
所以在你的代码中你可以这样做:
function generateDocument(e) {
var template = DocsList.getFileById(e.parameter.Templates);
var Sheet = SpreadsheetApp.getActiveSpreadsheet();// I moved this line a bit to have Sheet available
var row = e.parameter.row
var myDocID = template.makeCopy(Sheet.getRange('E'+row).getValue()+'-'+Sheet.getRange('D'+row).getValue()).getId();// this is a basic implementation to compose the name with content of column D and E separated by a hyphen... customize it the way you want.
...