我有一个Google幻灯片演示文稿,其中包含Google表格中的一些链接单元格/表格数据。目前,我必须手动单击每个链接的单元格/表来更新值。 我需要一个Google幻灯片脚本来自动更新/批量更新/刷新这些链接,以便值/表自动更新。这可能吗?
答案 0 :(得分:1)
是和否。
是的!那些可以批量更新:
function onOpen() {
SlidesApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Update Charts')
.addItem("Update now !!!!!", 'refreshCharts').addToUi();
}
function refreshCharts(){
var gotSlides = SlidesApp.getActivePresentation().getSlides();
for (var i = 0; i < gotSlides.length; i++) {
var slide = gotSlides[i];
var sheetsCharts = slide.getSheetsCharts();
for (var k = 0; k < sheetsCharts.length; k++) {
var shChart = sheetsCharts[k];
shChart.refresh();
}
}
}
来源:https://stackoverflow.com/a/48254442/
答案 1 :(得分:0)
有人让它起作用吗?
function refreshCharts(){
var gotSlides = SlidesApp.getActivePresentation().getSlides();
for (var i = 0; i < gotSlides.length; i++) {
var slide = gotSlides[i];
var sheetsCharts = slide.getSheetsCharts();
for (var k = 0; k < sheetsCharts.length; k++) {
var shChart = sheetsCharts[k];
shChart.refresh();
}
}
}
我刚刚收到此错误消息 ” 无法刷新图表。请验证该图表在Google表格中是否为有效图表。”
答案 2 :(得分:0)
以下示例设置假定有五张幻灯片以及一些链接的图表:
下面的功能将使用链接的图表更新幻灯片。 要使其正常运行,需要高级Google服务:Google Slides API https://developers.google.com/slides/。
您可以在Resources > Advanced Google Services... > Google Slides API
下的 Google Apps脚本IDE 中激活此高级功能。 API版本设置为v1
。如果您未启用它,脚本将抱怨Slides is not defined at updateSlideCharts(...)
有关Google Slides API v1中的RefreshSheetsChartRequest
的更多详细信息,请参见以下链接:https://developers.google.com/slides/reference/rest/v1/presentations/request#RefreshSheetsChartRequest
function updateSlideCharts() {
var presentation = SlidesApp.openById(YOUR_SLIDE_ID); //you need to get this slide id from your slide URL
//if empty
if (presentation == null) throw new Error('Presentation was not found');
Logger.log("%s id = %s", presentation.getName(), presentation.getId());
var slides = presentation.getSlides();
if (slides == null) throw new Error('Slides were not found');
Logger.log("Total of slides in %s: %d", presentation.getName(), slides.length);
var presentationId = presentation.getId();
var presentationRndChartId = slides[1].getSheetsCharts()[0].getObjectId();
Logger.log("ObjectId of \"%s\": %s", slides[1].getSheetsCharts()[0].getTitle(), presentationRndChartId);
var presentationImeChartId = slides[2].getSheetsCharts()[0].getObjectId();
Logger.log("ObjectId of \"%s\": %s", slides[2].getSheetsCharts()[0].getTitle(), presentationImeChartId);
var presentationPmoChartId = slides[3].getSheetsCharts()[0].getObjectId();
Logger.log("ObjectId of \"%s\": %s", slides[3].getSheetsCharts()[0].getTitle(), presentationPmoChartId);
var presentationStaffChartId = slides[4].getSheetsCharts()[0].getObjectId();
Logger.log("ObjectId of \"%s\": %s", slides[4].getSheetsCharts()[0].getTitle(), presentationStaffChartId);
var requests = [{
refreshSheetsChart: {
objectId: presentationRndChartId
}
}];
// Execute the request.
var batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
Logger.log('Refreshed linked Sheets charts for \"%s\"', slides[1].getSheetsCharts()[0].getTitle());
requests = [{
refreshSheetsChart: {
objectId: presentationImeChartId
}
}];
// Execute the request.
batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
Logger.log('Refreshed linked Sheets charts for \"%s\"', slides[2].getSheetsCharts()[0].getTitle());
requests = [{
refreshSheetsChart: {
objectId: presentationPmoChartId
}
}];
// Execute the request.
batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
Logger.log('Refreshed linked Sheets charts for \"%s\"', slides[3].getSheetsCharts()[0].getTitle());
var requests = [{
refreshSheetsChart: {
objectId: presentationStaffChartId
}
}];
// Execute the request.
var batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
Logger.log('Refreshed linked Sheets charts for \"%s\"', slides[4].getSheetsCharts()[0].getTitle());
}
答案 3 :(得分:0)
截至 2021 年 8 月,链接表仍然没有 .refresh()
函数,但如果您的用例允许您在编写脚本时知道源电子表格和范围,您可以通过阅读文本更新链接表值、字体颜色、字体样式等,从电子表格中读取并将它们写入表格。像这样:
function updateSheetsChart()
{
// The range on the source spreadsheet
var sourceRange = SpreadsheetApp.openById(SOURCE_SPREADSHEET_ID).getRange(SOURCE_TABLE_RANGE)
var source = {
'values': sourceRange.getDisplayValues(),
'backgrounds': sourceRange.getBackgrounds(),
'textStyles': sourceRange.getTextStyles(),
'fontColors': sourceRange.getFontColors()
}
// The linked table on the presentation
var table = SlidesApp.getActivePresentation().getPageElementById(SHEETS_TABLE_OBJECT_ID).asTable()
var columnCount = table.getNumColumns()
var rowCount = table.getNumRows()
for (var col = 0; col < columnCount; col++)
{
for (var row = 0; row < rowCount; row++)
{
var cell = table.getCell(row, col)
// Cell text
var cellText = cell.getText()
cellText.setText(source.values[row][col])
// Background color
cell.getFill().setSolidFill(source.backgrounds[row][col])
// Font style (bold)
var cellTextStyle = cellText.getTextStyle()
cellTextStyle.setBold(source.textStyles[row][col].isBold())
// Text color
cellTextStyle.setForegroundColor(source.fontColors[row][col])
}
}
}
您可以为字体大小、斜体、链接 URL 等添加更多行。请参阅 Range、Sheets TextStyle 和 Slides TextStyle 类的文档以了解相应方法。
有很多限制:例如,这不会复制完整的富文本(一个单元格中的多种颜色或字体样式)。
另请参阅 this answer 以获得更简单的解决方法(但它不会传输单元格格式)。