我在空幻灯片中插入一张表。然后我用不同的文本填写它。在此之后我想得到桌子的高度。 .getHeight的结果为null。 .getHeight方法适用于形状和图像,但不适用于表格。如何获得桌子高度?
function getTableHeight(){
var presentation = SlidesApp.openById('myPresentationId');
var slides = presentation.getSlides();
var slideId = 'mySlideId';
var slide = slides.filter(function(e){return e.getObjectId() == slideId})[0];
slide.insertTable(5, 5);
var tableHeight = slide.getTables()[0].getHeight();
Logger.log(tableHeight);
}
这是完整的脚本。它运行正常,但是当幻灯片上没有足够的空间时,我需要一个功能来分割导入的表。
function getTableFromSpreadsheetoToSlides(){
var presentation = SlidesApp.openById('myPresentationId');
var slideId = "mySlideId";
var slides = presentation.getSlides();
var slide = slides.filter(function(e){return e.getObjectId() == slideId})[0];
var spreadsheet = SpreadsheetApp.openById('mySpreadsheetId');
var sheet = spreadsheet.getSheetByName('ActionItems');
var numRows = sheet.getDataRange().getNumRows();
var numColumns = sheet.getDataRange().getNumColumns();
var values = sheet.getSheetValues(1, 1, numRows, numColumns).valueOf();
var table = slide.getTables()[0];
var numRowsToDelete = table.getNumRows() - 1;
// remove old tableRows except the table head
for (i = numRowsToDelete; i > 0; i--) {
table.getRow(i).remove();
}
// new imported table rows append to the existing table head
for ( var i = 1; i < numRows; i++ ) {
table.appendRow();
for ( var j = 0; j < numColumns; j++ ) {
var text = table.getCell(i, j).getText().setText(values[i][j]);
}
// HERE I need a query how long the table in slide is. If it is longer/higher than the slide is, it should be split and the next row should go to a new slide
}
}
答案 0 :(得分:1)
getHeight()
方法与Tables不兼容。
请参阅开发者页面上的限制图表:https://developers.google.com/apps-script/guides/slides/moving-elements
答案 1 :(得分:1)
这个解决方法怎么样? @Randy D说,getHeight()
不能用于表。所以我建议使用Slides API解决方法。
Slides.Presentations.Pages.get()
检索幻灯片信息时,还会包含表格信息。在信息中,还包括表格的高度和宽度。修改后的脚本反映如下。
要使用此脚本,请在Advanced Google Services和API console启用幻灯片API。
function getTableHeight(){
var presentation = SlidesApp.openById('myPresentationId');
var slides = presentation.getSlides();
var slideId = 'mySlideId';
var slide = slides.filter(function(e){return e.getObjectId() == slideId})[0];
slide.insertTable(5, 5);
// Added scripts from here
var pageElements = Slides.Presentations.Pages.get(presentation.getId(), slideId).pageElements;
var result = [];
for (var i in pageElements) {
var rowHeight = 0;
var rowWidth = 0;
if (pageElements[i].table) {
for (var j in pageElements[i].table.tableRows) {
rowHeight += pageElements[i].table.tableRows[j].rowHeight.magnitude / 12700;
}
for (var k in pageElements[i].table.tableColumns) {
rowWidth += pageElements[i].table.tableColumns[k].columnWidth.magnitude / 12700;
}
result.push({Height: rowHeight, Width: rowWidth});
}
}
Logger.log(result)
}
pageElements[i].table.tableRows[j].rowHeight.magnitude
和pageElements[i].table.tableColumns[k].columnWidth.magnitude
检索到的每个值都需要除以12,700。function getTableFromSpreadsheetoToSlides(){
var presentation = SlidesApp.openById('myPresentationId');
var slideId = "mySlideId";
var slides = presentation.getSlides();
var slide = slides.filter(function(e){return e.getObjectId() == slideId})[0];
var spreadsheet = SpreadsheetApp.openById('mySpreadsheetId');
var sheet = spreadsheet.getSheetByName('ActionItems');
var numRows = sheet.getDataRange().getNumRows();
var numColumns = sheet.getDataRange().getNumColumns();
var values = sheet.getSheetValues(1, 1, numRows, numColumns).valueOf();
var table = slide.getTables()[0];
var numRowsToDelete = table.getNumRows() - 1;
// remove old tableRows except the table head
for (i = numRowsToDelete; i > 0; i--) {
table.getRow(i).remove();
}
// From here, I added and modified script.
var pageHeight = Slides.Presentations.get(presentation.getId()).pageSize.height.magnitude / 12700;
var rowHeight = getTableHeight(presentation.getId(), slideId);
var additionalRows = Math.floor(pageHeight / rowHeight);
var remainingRows = numRows - additionalRows; // You can add the remaining rows using this.
for ( var i = 1; i < additionalRows; i++ ) { // Modified
table.appendRow();
for ( var j = 0; j < numColumns; j++ ) {
var text = table.getCell(i, j).getText().setText(values[i][j]);
}
}
}
function getTableHeight(slidesId, slideId) {
var pageElements = Slides.Presentations.Pages.get(slidesId, slideId).pageElements;
var result = [];
for (var i in pageElements) {
var rowHeight = 0;
var rowWidth = 0;
if (pageElements[i].table) {
for (var j in pageElements[i].table.tableRows) {
rowHeight += pageElements[i].table.tableRows[j].rowHeight.magnitude / 12700;
}
for (var k in pageElements[i].table.tableColumns) {
rowWidth += pageElements[i].table.tableColumns[k].columnWidth.magnitude / 12700;
}
result.push({Height: rowHeight, Width: rowWidth});
}
}
return result[0].Height;
}
getTableHeight()
可用于包含多个表格的幻灯片。但在这种情况下,它假设幻灯片中有一个表。saveAndClose()
也无法做到。我不知道原因。