【问题标题】:Is it possible to output data from each row of a Google Sheet into a separate Google Doc是否可以将 Google Sheet 每一行的数据输出到单独的 Google Doc 中
【发布时间】:2021-02-16 14:42:39
【问题描述】:

我什至不确定这是否可行,但我有一个 Google 表格,我打算使用带有模板文档的邮件合并方法将它合并到一个 Google 文档中。

我想知道是否可以为工作表的每一行生成一个新的 Google 文档。

目前我使用的脚本将从工作表中获取所有行,将它们传递给模板文件,然后将它们输出到我的最终文档中。工作表的每一行将由最终文档中的分页符分隔。但理想情况下,我希望每一行都在一个单独的文档中。

我当前的脚本代码是

function myFunction() {

var docTemplateId = "docTemplateId";
var docFinalId = "docFinalId";
var wsId = "wsId";

var docTemplate = DocumentApp.openById(docTemplateId);
var docFinal = DocumentApp.openById(docFinalId);
var ws = SpreadsheetApp.openById(wsId).getSheetByName("data");

var data = ws.getRange(2,1,ws.getLastRow()-1,7).getValues();
var templateParagraphs = docTemplate.getBody().getParagraphs();

docFinal.getBody().clear();

data.forEach(function(r){
createMailMerge(r[0],r[3],r[4],templateParagraphs, docFinal);

});
}
function createMailMerge(titre, description, suivantes,templateParagraphs, docFinal) {

templateParagraphs.forEach(function(p) {
var elType = p.getType();

if(elType == "PARAGRAPH"){
docFinal.getBody().appendParagraph(
p
.copy()
.replaceText("{titre}", titre)
.replaceText("{description}", description)
.replaceText("{suivantes}", suivantes)
);
}
else if(elType == "LIST_ITEM") {
 docFinal.getBody().appendListItem(
 p
 .copy()
 .replaceText("{titre}", titre)
 .replaceText("{description}", description)
 .replaceText("{suivantes}", suivantes)
 ).setGlyphType(DocumentApp.GlyphType.BULLET);
 }

});
docFinal.getBody().appendPageBreak();

};

任何人都可以帮助我实现这一目标。我知道我可以为每一行创建文档,然后将它们添加为变量,但也许有更有效的方法。

【问题讨论】:

    标签: google-apps-script google-sheets google-docs mailmerge


    【解决方案1】:

    使用 Drive Advanced 服务的替代方法

    您可以尝试完全在 HTML 中创建文档,然后使用 Advanced Drive Service 创建包含内容的文件,而不是向DocumentApp 发出多个请求以一点一点地构建文档.这应该会大大加快执行速度。

    function createFile(title, html) {
      var resource = {
        title: title,
        mimeType: MimeType.GOOGLE_DOCS
        // parents: [{ id: folderId }]  You can add this to make it go in a folder.
      };
      Drive.Files.insert(resource, html);
    }
    
    
    function test(){
      // Build up your HTML as a string and then:
      var html = HtmlService.createHtmlOutput("<h1>Hello World</h1><p>Created from Apps Script using advanced services</p>")
      
      createFile("Test Drive Advanced Service", html)
    }
    

    运行test 函数会在此处生成此文档:

    如您所见,如果您将 HTML 内容上传到云端硬盘,但您将其指定为 Google 文档,那么它会毫无问题地进行转换。这对于像您这样的情况非常方便。

    设置

    请务必先在此处激活 Advanced Drive 服务:

    查看“服务”旁边的“+”按钮,单击并选择 Drive API。

    请注意,这使用 Drive API v2 v3。

    参考文献

    【讨论】:

    • 谢谢,我试试看
    【解决方案2】:

    我认为这实际上已经是您的最佳选择。我不确定是否有实际批量处理的方法,但由于您一次访问一行并且有一个模板,所以这种方法已经很好了。

    data.forEach(function (r) {
      // Assuming titre is unique, use it as doc name
      // Or combine the parameters to produce a unique doc name
      createMailMerge(r[0], r[3], r[4], templateParagraphs, DocumentApp.create(r[0]));
    });
    

    至于createMailMerge,您可以删除appendPageBreak 行,因为我们已经没有将数据附加到一个文档中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      相关资源
      最近更新 更多