简体   繁体   English

使用谷歌应用脚本将数据从谷歌表格中提取到一个谷歌文档文件中

[英]Pulling data into ONE google docs file from a google sheet using google app script

Trying to pull all the data from one google sheet and automatically populate one google doc, instead of four separate files after the data pull.试图从一张谷歌表格中提取所有数据并自动填充一个谷歌文档,而不是在数据提取后填充四个单独的文件。 Missing something here which I'm scratching my head about and can't seem to find anything online.在这里遗漏了一些我正在挠头并且似乎无法在网上找到任何东西的东西。

This is my code so far:到目前为止,这是我的代码:

function createNewGoogleDocs() {
  //This value should be the id of your document 
  const googleDocTemplate = DriveApp.getFileById('1kVXtatdcdlKRYzDADnIckSYcg8N3SIixn-6lEHsRMbk');
  
  //This value should be the id of the folder where you want your completed documents stored
  const destinationFolder = DriveApp.getFolderById('1ZmfdojPXdBkW93EECH9rd9Vt06Cqx7tI')

  //Here we store the sheet as a variable
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Data')
  
  //Now we get all of the values as a 2D array. Merging sheet data into neccessary value type to work with.
  const rows = sheet.getDataRange().getValues();
  
  //Start processing each spreadsheet row (each array)
  rows.forEach(function(row, index){
    //Here we check if this row is the headers, if so we skip it
    if (index === 0) return;
    //Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
    if (row[5]) return;
    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder
    const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} Article Details` , destinationFolder)
    //Once we have the copy, we then open it using the DocumentApp
    const doc = DocumentApp.openById(copy.getId())
    //All of the content lives in the body, so we get that for editing
    const body = doc.getBody();
    //In this line we do some friendly date formatting
    const friendlyDate = new Date(row[1]).toLocaleDateString();
    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    body.replaceText('{{Headline}}', row[0]);
    body.replaceText('{{Timestamp}}', friendlyDate);
    body.replaceText('{{Article}}', row[2]);
    body.replaceText('{{CODR}}', row[3]);
    body.replaceText('{{URL}}', row[4]);
    
    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
    //Store the url of our new document in a variable
    const url = doc.getUrl();
    //Write that value back to the 'Document Link' column in the spreadsheet. 
    sheet.getRange(index + 1, 6).setValue(url)
    
  })
  
}

Appending Template Data just prior to replacing data so that all data in placed in one copy.在替换数据之前附加模板数据,以便所有数据都放在一个副本中。

function createNewGoogleDocs() {
  const googleDocTemplate = DriveApp.getFileById('1OuxpEp8bzD7ndFZ9SUtfqmWeat-4L5SDvjcZW5hsJ7g');
  const destinationFolder = DriveApp.getFolderById(gobj.globals.folder1id);
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0')
  const [h, ...vs] = sh.getDataRange().getValues();
  const date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy/MM/dd")
  const copy = googleDocTemplate.makeCopy(`${date} Article Details`, destinationFolder)
  vs.forEach((r, i) => {
    if (!r[5]) {
      appendToTemplate(i + 1, copy.getId())
      const doc = DocumentApp.openById(copy.getId());
      const url = doc.getUrl();
      const body = doc.getBody();
      body.replaceText(`{{Headline${i + 1}}}`, r[0]);
      body.replaceText(`{{TimeStamp${i + 1}}}`, new Date(r[1]).toLocaleDateString());
      body.replaceText(`{{Article${i + 1}}}`, r[2]);
      body.replaceText(`{{CODR${i + 1}}}`, r[3]);
      body.replaceText(`{{URL${i + 1}}}`, r[4]);
      doc.saveAndClose();
      sh.getRange(i + 2, 6).setValue(url);
    }
  });
}

You will have to customize the below code to meet your precise needs您将必须自定义以下代码以满足您的精确需求

function appendToTemplate(n, id = "1OuxpEp8bzD7ndFZ9SUtfqmWeat-4L5SDvjcZW5hsJ7g") {
  const d = DocumentApp.openById(id);
  const b = d.getBody();
  const s = `Line 1 is {{Headline${n}}}\nLine 2 is {{TimeStamp${n}}}\nLine 3 is {{Article${n}}}\nLine 4 is {{CODR${n}}}\nLine 5 is {{URL${n}}}`;
  d.appendParagraph(s);
  d.saveAndClose();
  const end = "is near";
}

Starting Data:起始数据:

A一种 B C C D E
1 1个 Headline标题 Date日期 article文章 codr编码器 url url
2 2个 Headline1标题1 11/1/2022 11/1/2022 article1第1条 codr1编码器1 url1网址 1
3 3个 Headline2标题2 11/2/2022 11/2/2022 articel2第2条 code2代码2 url2网址2
4 4个 Headline3标题3 11/3/2022 11/3/2022 article2第2条 codr2编码器2 url3网址3
5 5个 Headline4标题4 11/4/2022 11/4/2022 articel3第 3 条 code3代码3 url4网址4
6 6个 Headline5标题5 11/5/2022 11/5/2022 article3第三条 codr3编码器3 url5网址5

Final Document:最终文件:

Line 1 is Headline1
Line 2 is 11/1/2022
Line 3 is article1
Line 4 is codr1
Line 5 is url1
Line 1 is Headline2
Line 2 is 11/2/2022
Line 3 is articel2
Line 4 is code2
Line 5 is url2
Line 1 is Headline3
Line 2 is 11/3/2022
Line 3 is article2
Line 4 is codr2
Line 5 is url3
Line 1 is Headline4
Line 2 is 11/4/2022
Line 3 is articel3
Line 4 is code3
Line 5 is url4
Line 1 is Headline5
Line 2 is 11/5/2022
Line 3 is article3
Line 4 is codr3
Line 5 is url5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用脚本应用从Google表格中的单元格提取日期 - Pulling Date From Cell in Google Sheet Using Script App 使用工作表和应用程序脚本在谷歌文档中批量查找和替换 - bulk find and replace in google docs using sheet and app script 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script? 使用 Google App 脚本将数据从一张工作表复制到另一张工作表时执行缓慢 - Slow Execution for Copying Data from One Sheet to Another Using Google App Script 使用谷歌脚本从一列搜索谷歌表格数据的方法? - Method search on google sheet data from one column using google script? 将一张工作表中的原始数据排序到Google App脚本中新工作表中的单独标签中 - Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 如何使用谷歌应用脚本在谷歌文档中获取字符串的索引 - How to get the index of a string in google docs using google App Script 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM