一次性下载所有 Google 张图表?
Download all Google sheets charts in one shot?
我有一个 sheet 有超过 30 个图表,我的员工需要每小时下载所有图表,大约 24/7,这对他们来说需要很多时间。所以我的问题是,是否有任何方法可以通过单击或自动方式下载 sheet 中的所有图表?
我相信你的目标如下。
您想从 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文件下载到哪里?所以在这个回答中,我想提出以下两种模式。
- 将图表图像作为 PDF 下载到 Google 驱动器。
- 将图表图像作为 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);
}
模式二:
在此模式中,图表图像以 PDF 格式下载到本地 PC。此方法来自。在这种情况下,当download()
为运行时,在打开的对话框中使用Javascript将PDF文件下载到本地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
请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,作为 HTML。
<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>
注:
- 以上模式是简单的示例脚本。所以请根据自己的实际情况修改。
参考文献:
我有一个 sheet 有超过 30 个图表,我的员工需要每小时下载所有图表,大约 24/7,这对他们来说需要很多时间。所以我的问题是,是否有任何方法可以通过单击或自动方式下载 sheet 中的所有图表?
我相信你的目标如下。
您想从 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文件下载到哪里?所以在这个回答中,我想提出以下两种模式。
- 将图表图像作为 PDF 下载到 Google 驱动器。
- 将图表图像作为 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);
}
模式二:
在此模式中,图表图像以 PDF 格式下载到本地 PC。此方法来自download()
为运行时,在打开的对话框中使用Javascript将PDF文件下载到本地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
请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,作为 HTML。
<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>
注:
- 以上模式是简单的示例脚本。所以请根据自己的实际情况修改。