从谷歌表格单元格获取文本到谷歌文档

时间:2021-01-06 16:16:49

标签: google-apps-script google-sheets

我有一个谷歌表,我们每周都会创建一个新版本,并更新表名称。在这张表中,我们有一个带有注释的单元格。该单元格是固定的。如何从所有工作表中提取数据,让我们说以 Reporting Weekly 开头从该单元格获取文本并将其推送到谷歌文档。

1 个答案:

答案 0 :(得分:1)

function copyNotes() {
  const prefix="Report Weekly";
  const fldrid="";//report folder id
  const name=Utilities.formatString('ReportSummary:%s',Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E-yy.MM.dd.HH.mm.ss"));//you may wish to change name formatting for the new document
  const fldr=DriveApp.getFolderById(fldrid);
  const fileid=DocumentApp.create(name).getId();//creating the document
  const doc=DocumentApp.openById(fileid);//open document
  const notecell='A1';//The cell where the notes are found
  const ss=SpreadsheetApp.getActive();
  const shts=ss.getSheets().filter(s=>s.getName().startsWith(prefix));//filter out unwanted sheets
  shts.forEach(sh=>{doc.getBody().appendParagraph(sh.getRange(notecell).getValue())});//read notecells of all sheets and append to document
  doc.saveAndClose();//save document in root
  Drive.Files.update({"parents": [{"id": fldr.getId()}]}, fileid);//move to correct folder
}

注意:您需要启用高级 Drive API 才能运行此功能。