2013-08-07 492 views
0

我正在嘗試編輯此「從模板生成」腳本。腳本的功能是基於一個模板,它將信息從電子表格輸入到文檔。我喜歡它是如何工作的,只是它將文檔命名爲模板的副本,然後是行號。這對於我正在嘗試使用它的效率並不高。我的問題是,我將如何能夠執行以下任一選項:創建文檔時的文檔名稱

答:名稱必須基於該行中的某個單元格。例如,有一個名爲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

的makeCopy方法包括這種可能性給一個名稱複製,自動完成顯示清楚

enter image description here 因此,在你的代碼,你可以做這樣的:

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. 
    ...