Google apps script Google Sheets图表的透明背景以PNG格式保存到驱动器中
试图将图表保存到具有透明背景的驱动器中时遇到问题。我可以Google apps script Google Sheets图表的透明背景以PNG格式保存到驱动器中,google-apps-script,charts,google-sheets,google-visualization,Google Apps Script,Charts,Google Sheets,Google Visualization,试图将图表保存到具有透明背景的驱动器中时遇到问题。我可以.setOption('backgroundColor','transparent')但这不会设置图表所在“容器”的背景,因此图表仍显示为白色背景 有人知道如何参考和设置图表所在容器的背景色吗 谢谢 尼克 函数saveChartstoGdrive(){ //从当前电子表格中获取图表 var targetspreadsheet=SpreadsheetApp.getActiveSpreadsheet(); var sheet=targetsp
.setOption('backgroundColor','transparent')代码>但这不会设置图表所在“容器”的背景,因此图表仍显示为白色背景
有人知道如何参考和设置图表所在容器的背景色吗
谢谢
尼克
函数saveChartstoGdrive(){
//从当前电子表格中获取图表
var targetspreadsheet=SpreadsheetApp.getActiveSpreadsheet();
var sheet=targetspreadsheet.getSheetByName(“销售图”);
var charts=sheet.getCharts();
var chartBlobs=新数组(charts.length);
var filePrefix=“scrlxz”;
//循环浏览图表并将每个图表保存到gdrive
对于(var i=0;i我相信你的目标如下
- 您希望修改工作表上具有透明背景的图表
- 您希望将修改后的图表导出为具有透明背景的PNG文件
- 您希望使用谷歌应用程序脚本实现这一点
修改点:
- 最近,我报告了关于。当我看到你的问题时,我认为这份报告可能有用。这份报告的流程用于你的情况,如下所示
- 检索图表并修改图表
- 更新图表
- 创建新的Google幻灯片作为临时文件
- 将幻灯片的背景色更改为透明
- 插入修改后的图表。图表的背景色为透明
- 使用alpha通道将幻灯片导出为PNG
- 图表将返回到原始图表
- 删除临时文件
- 将导出的PNG数据创建为文件。在这种情况下,当该文件存在时,将覆盖该文件。当找不到该文件时,将该文件创建为新文件
- 而且,当我看到您的脚本时,现有的图表被修改为透明背景;
。在这种情况下,我发现修改后的图表仍然没有反映到newChart
。因此,在这个修改后的脚本中,图表由修改后的图表更新。检索到更新后的图表后,图表将返回到原始图表
而且,在这个修改后的脚本中,当文件名为fileName
的文件存在时,该文件将被新图表覆盖。当找不到该文件时,该文件将使用新图表作为新文件创建
当上述各点反映到脚本中时,它将变成如下所示
修改脚本:
请将以下脚本复制并粘贴到Google电子表格的脚本编辑器中。并且,此脚本假设工作表“销售图”中有该图表。请注意此操作。并且,请再次确认是否已在Advanced Google services中启用了驱动API
function saveChartstoGdrive() {
var targetspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = targetspreadsheet.getSheetByName('Sales Graph');
var charts = sheet.getCharts();
var chartBlobs = new Array(charts.length);
var filePrefix = "scrlxz";
for (var i = 0; i < charts.length; i++) {
// 1. Retrieve chart and modify chart.
var orgChart = charts[i]; // Added
var builderChart = charts[i].modify();
builderChart.setOption('width', 1400);
builderChart.setOption('height', 900);
builderChart.setOption('title', 'Updated!')
builderChart.setOption('backgroundColor', 'transparent');
var newChart = builderChart.build();
// --- I added below script.
// 2. Update chart.
sheet.updateChart(newChart); // It seems that this is required to be used for reflecting the settings.
// 3. Create new Google Slides as the temporal file.
var temp = SlidesApp.create("temp");
var slide = temp.getSlides()[0];
var id = temp.getId();
slide.getShapes().forEach(s => s.remove());
// 4. Change the background color of slide to the transparent.
slide.getBackground().setTransparent();
// 5. Insert the modified chart. The background color of chart is the transparent.
slide.insertSheetsChart(newChart);
temp.saveAndClose();
// 6. Export the slide as PNG with the alpha channel.
var url = `https://docs.google.com/feeds/download/presentations/Export?id=${id}&exportFormat=png`;
var blob = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}}).getBlob();
// 7. The chart is returned back to the original one.
sheet.updateChart(orgChart); // Here, the updated chart is returned back to the original one.
// 8. Remove the temporal file.
DriveApp.getFileById(id).setTrashed(true);
// --- I modified below script.
// 9. Create the exported PNG data as a file. In this case, when the file is existing, the file is overwritten. When the file is not found, the file is created as new file.
chartBlobs[i] = blob;
var fileName = filePrefix + [i] + ".png";
var contentBlob = chartBlobs[i];
var file = DriveApp.getFilesByName(fileName);
if (file.hasNext()) {
var fileId = file.next().getId();
var myVar = Drive.Files.update({ mimeType: 'image/png' }, fileId, contentBlob);
} else {
DriveApp.createFile(contentBlob.setName(fileName));
}
}
}
函数saveChartstoGdrive(){
var targetspreadsheet=SpreadsheetApp.getActiveSpreadsheet();
var sheet=targetspreadsheet.getSheetByName(“销售图”);
var charts=sheet.getCharts();
var chartBlobs=新数组(charts.length);
var filePrefix=“scrlxz”;
对于(var i=0;is.remove());
//4.将幻灯片的背景色更改为透明。
slide.getBackground().setTransparent();
//5.插入修改后的图表,图表背景色为透明。
幻灯片。插入表单沙尔特(新图表);
temp.saveAndClose();
//6.使用alpha通道将幻灯片导出为PNG。
变量url=`https://docs.google.com/feeds/download/presentations/Export?id=${id}&exportFormat=png`;
var blob=UrlFetchApp.fetch(url,{头:{授权:“承载者”+ScriptApp.getOAuthToken()});
//7.图表返回到原始图表。
sheet.updateChart(orgChart);//在这里,更新后的图表返回到原始图表。
//8.删除临时文件。
DriveApp.getFileById(id).setTrashed(true);
//---我修改了下面的脚本。
//9.将导出的PNG数据创建为文件。在这种情况下,当该文件存在时,将覆盖该文件。当找不到该文件时,将该文件创建为新文件。
ChartBlob[i]=blob;
var fileName=filePrefix+[i]+“.png”;
var contentBlob=chartBlobs[i];
var file=DriveApp.getFilesByName(文件名);
if(file.hasNext()){
var fileId=file.next().getId();
var myVar=Drive.Files.update({mimeType:'image/png'},fileId,contentBlob);
}否则{
createFile(contentBlob.setName(文件名));
}
}
}
结果:
当为下一页运行上述脚本时,将获得带有alpha通道的PNG文件
为了确认PNG文件的透明背景,作为一个示例,当创建的PNG文件插入到电子表格中时,它变成如下。您可以看到图像的背景是透明的
参考资料:
如果使用DriveApp.createFile保存文件,是否会获得相同的输出