嘗試將我的 DataTable(使用圖表服務)插入 Google 幻燈片時遇到問題。
當我嘗試獲取圖表的 Blob 或將圖表獲取為“image/png”或 jpeg 等時,就會出現問題。
這是代碼:
function TablesConstructor(slide_obj){
//slide_obj is an object that contains all the information needed.
//Let's open the file
var slide_file = SlidesApp.openById(slide_obj.slide_id);
//Get the Slides
var slides = slide_file.getSlides();
var slides_num = slides.length;
//Get the Slide N-1, and it's images
var slide_images = slides[slides_num - 2].getImages();
var slide_image_1 = slide_images[0];
//Create Data Table chart
var table_data = Charts.newDataTable()
.addColumn(Charts.ColumnType.NUMBER, "Col1")
.addColumn(Charts.ColumnType.NUMBER, "Col2")
.addColumn(Charts.ColumnType.NUMBER, "Col3")
.addColumn(Charts.ColumnType.NUMBER, "Col4")
//Populate data table chart with the 2D array inside my object
var rows = slide_obj.eem_table_data.length <= 10 ? slide_obj.eem_table_data.length : 10;
for(var i = 0; i < rows; i ){
table_data.addRow([slide_obj.eem_table_data[i][0],
slide_obj.eem_table_data[i][1],
slide_obj.eem_table_data[i][2],
slide_obj.eem_table_data[i][3],
);
}
//Build the table
var table_chart = Charts.newTableChart().setDataTable(table_data).build();
//ATTEMPT 1
//Get the chart Blob and replace the image with the blob
var image_blob = table_chart.getBlob();
slide_image_1.replace(image_blob, true);
slide_file.saveAndClose();
}
我還提出了一些其他方法,不幸的是也沒有奏效
//ATTEMPT 2
//Set the ContentType to jpeg
var table_jpeg = table_chart.getBlob().setContentType('image/jpeg')
slide_image_1.replace(table_jpeg, true);
//ATTEMP 3
//Replace a Shape with the table chart
slide_elements.forEach(function(pageElement){
Logger.log(pageElement.getPageElementType());
if(pageElement.asShape().getShapeType() !== "UNSUPPORTED"){
if (pageElement.asShape().getShapeType() == "RECTANGLE"){
pageElement.asShape().replaceWithImage(table_chart.getAs('image/jpeg'));
}
}
});
我的幻燈片(條形圖)中有其他型別的圖表,它們作業得很好,但是對于這個特定的圖表,我得到的錯誤訊息是:
例外:很抱歉,發生服務器錯誤。請稍等,然后重試。
這個,在線
var image_blob = table_chart.getBlob();
uj5u.com熱心網友回復:
根據你的建議,我發布了這個。在這種情況下,請使用setDimensions如下設定尺寸。
從:
var table_chart = Charts.newTableChart().setDataTable(table_data).build();
到:
var table_chart = Charts.newTableChart().setDataTable(table_data).setDimensions(640, 480).build();
參考:
- 設定尺寸(寬度,高度)
uj5u.com熱心網友回復:
用這個答案解決了它:
Google Script:如何在電子郵件中發送表格圖表?
需要使用 宣告圖表的大小setDimensions()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/432773.html
