简体   繁体   English

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

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

I have a sheet that has more than 30 charts and my employees need to download all charts every hour, around 24/7 which takes much time for them.我有一张包含 30 多个图表的表格,我的员工需要每小时下载所有图表,大约 24/7,这对他们来说会花费很多时间。 so my question is, is there is any way to download all charts in the sheets with one click or in an automated way?所以我的问题是,是否有任何方法可以通过单击或自动方式下载工作表中的所有图表?

I believe your goal is as follows.我相信你的目标如下。

  • You want to retrieve all charts from all sheets in a Google Spreadsheet.您想要从 Google 电子表格的所有工作表中检索所有图表。

  • You want to download all chart images as a PDF file.您想要将所有图表图像下载为 PDF 文件。

  • From your following replying, I understood like above从您的以下回复中,我的理解与上述相同

     > 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

But, from your question and replying, I couldn't understand where you want to download the PDF file to?但是,从你的问题和回复中,我无法理解你要将 PDF 文件下载到哪里? So in this answer, I would like to propose the following 2 patterns.所以在这个回答中,我想提出以下两种模式。

  1. Download chart images as a PDF to Google Drive.将图表图像作为 PDF 下载到 Google 云端硬盘。
  2. Download chart images as a PDF to a local PC.将图表图像作为 PDF 下载到本地 PC。

Pattern 1:模式 1:

In this pattern, the chart images are downloaded as a PDF to Google Drive.在此模式中,图表图像作为 PDF 下载到 Google 云端硬盘。 Please copy and paste the following script to the script editor of Google Spreadsheet.请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中。 In this case, when myFunction() is run, the PDF file is downloaded to Google Drive.在这种情况下,当运行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);
}

Pattern 2:模式 2:

In this pattern, the chart images are downloaded as a PDF to a local PC.在此模式中,图表图像作为 PDF 下载到本地 PC。 This method is from this answer .这个方法来自这个答案 In this case, when download() is run, the PDF file is downloaded to the local PC using Javascript on the opened dialog.在这种情况下,当运行download()时,PDF 文件将在打开的对话框中使用 Javascript 下载到本地 PC。

But, in this pattern, it is required to run the script by the user on browser.但是,在这种模式下,需要用户在浏览器上运行脚本。 Because Javascript is used on the browser.因为浏览器上使用的是Javascript。 Please be careful this.请注意这一点。

Code.gs

Please copy and paste the following script to the script editor of Google Spreadsheet as a script.请将以下脚本作为脚本复制并粘贴到 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

Please copy and paste the following script to the script editor of Google Spreadsheet as 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>

Note:笔记:

  • The above pattenrs are the simple sample scripts.上面的 pattenrs 是简单的示例脚本。 So please modify them for your actual situation.所以请根据您的实际情况修改它们。

References:参考:

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

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