繁体   English   中英

一次性下载所有 Google 表格图表?

[英]Download all Google sheets charts in one shot?

我有一张包含 30 多个图表的表格,我的员工需要每小时下载所有图表,大约 24/7,这对他们来说会花费很多时间。 所以我的问题是,是否有任何方法可以通过单击或自动方式下载工作表中的所有图表?

我相信你的目标如下。

  • 您想要从 Google 电子表格的所有工作表中检索所有图表。

  • 您想要将所有图表图像下载为 PDF 文件。

  • 从您的以下回复中,我的理解与上述相同

     > the current issue is that my team is working on hourly performance for the agents and we have many teams and we have charts for performance and quality so the total charts that need to be downloaded and sent to each team are around 30 charts which translates to 30 downloads to each chart image. so this is happening every hour for 24 hours for the whole day which is 24 hours across week, month, year. so imagine if each hour takes 5 minutes to just download this will result in 120 mins or 2 hours a day, in a month this will cost us 14 hours per week, 60 hours per month. > I know that I can add the charts to Google slides and export them as pdf, but the managers wouldn't prefer that idea that much

但是,从你的问题和回复中,我无法理解你要将 PDF 文件下载到哪里? 所以在这个回答中,我想提出以下两种模式。

  1. 将图表图像作为 PDF 下载到 Google 云端硬盘。
  2. 将图表图像作为 PDF 下载到本地 PC。

模式 1:

在此模式中,图表图像作为 PDF 下载到 Google 云端硬盘。 请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中。 在这种情况下,当运行myFunction()时,PDF 文件将下载到 Google 云端硬盘。

function myFunction() {
  const outputFilename = "sample.pdf";

  // 1. Retrieve all charts in a Google Spreadsheet.
  const charts = SpreadsheetApp.getActiveSpreadsheet().getSheets().flatMap(s => s.getCharts());

  // 2. Create new Google Slides as a temporal file.
  const s = SlidesApp.create("temp");

  // 3. Put the charts to each slide.
  let slide = s.getSlides()[0];
  slide.getShapes().forEach(e => e.remove());
  charts.forEach((c, i, a) => {
    slide.insertSheetsChart(c).alignOnPage(SlidesApp.AlignmentPosition.CENTER);
    if (i < a.length - 1) slide = s.appendSlide();
  });
  s.saveAndClose();

  // 4. Output Google Slides as a PDF data.
  const file = DriveApp.getFileById(s.getId());
  DriveApp.createFile(file.getBlob().setName(outputFilename));
  
  // 5. Remove the temporal file of Google Slides.
  file.setTrashed(true);
}

模式 2:

在此模式中,图表图像作为 PDF 下载到本地 PC。 这个方法来自这个答案 在这种情况下,当运行download()时,PDF 文件将在打开的对话框中使用 Javascript 下载到本地 PC。

但是,在这种模式下,需要用户在浏览器上运行脚本。 因为浏览器上使用的是Javascript。 请注意这一点。

Code.gs

请将以下脚本作为脚本复制并粘贴到 Google 电子表格的脚本编辑器中。

function download() {
  const html = HtmlService.createHtmlOutputFromFile("index");
  SpreadsheetApp.getUi().showModalDialog(html, "sample");
}

function downloadFile() {
  const outputFilename = "sample.pdf";

  // 1. Retrieve all charts in a Google Spreadsheet.
  const charts = SpreadsheetApp.getActiveSpreadsheet().getSheets().flatMap(s => s.getCharts());

  // 2. Create new Google Slides as a temporal file.
  const s = SlidesApp.create("temp");

  // 3. Put the charts to each slide.
  let slide = s.getSlides()[0];
  slide.getShapes().forEach(e => e.remove());
  charts.forEach((c, i, a) => {
    slide.insertSheetsChart(c).alignOnPage(SlidesApp.AlignmentPosition.CENTER);
    if (i < a.length - 1) slide = s.appendSlide();
  });
  s.saveAndClose();

  // 4. Output Google Slides as a blob of PDF data.
  const file = DriveApp.getFileById(s.getId());
  const blob = file.getBlob().setName(outputFilename);
  
  // 5. Remove the temporal file of Google Slides.
  file.setTrashed(true);

  // 6. Return data as base64.
  return {data: `data:${MimeType.PDF};base64,${Utilities.base64Encode(blob.getBytes())}`, filename: outputFilename};
}

index.html

请将以下脚本作为 HTML 复制并粘贴到 Google 电子表格的脚本编辑器中。

<script>
google.script.run
  .withSuccessHandler(({ data, filename }) => {
    if (data && filename) {
      const a = document.createElement("a");
      document.body.appendChild(a);
      a.download = filename;
      a.href = data;
      a.click();
    }
    google.script.host.close();
  })
.downloadFile();
</script>

笔记:

  • 上面的 pattenrs 是简单的示例脚本。 所以请根据您的实际情况修改它们。

参考:

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM