如果图表没有数据,如何从 Google 幻灯片中删除 Google 表格图表
How to remove Google Sheets Chart from Google Slide if Chart has no data
我正在使用应用脚本将表格中的嵌入图表插入到幻灯片中。但如果 Google 表格上的图表 empty/has 没有数据,我想从幻灯片中删除图表。
我只想在没有数据时从幻灯片中删除图表。但是如果有数据就保留图表
你能帮我添加正确的行来创建这个条件吗?
这是我的图表代码:
function onOpen() {
// Get the Ui object.
var ui = SpreadsheetApp.getUi();
// Create a custom menu.
ui.createMenu('Present Data')
.addItem("Generate Report","generateLandingPagesReport")
.addToUi();
}
function generateLandingPagesReport() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Copy of Overall Performance 1');
var values = sheet.getRange('A2:J23').getValues();
var chartRegion1 = sheet.getCharts()[0];
var chartGender1 = sheet.getCharts()[1];
// Access the template presentation
var templateId = "1bXAYGCKkpZhksXz8gTCgFYbNoI1BIhAZakd68VlXHeo";
var template = SlidesApp.openById(templateId);
var templateSlides = template.getSlides();
// Create a Slides presentation, removing the default
// title slide.
var presentationTitle =
ss.getName() + " Presentation";
var slides = SlidesApp.create(presentationTitle);
var defaultSlides = slides.getSlides();
defaultSlides.forEach(function(slide) {
slide.remove()
});
var defaultSlide = defaultSlides [1];
// Insert slides from template
var index = 0;
templateSlides.forEach(function(slide) {
var newSlide = slides.insertSlide(index);
var elements = slide.getPageElements();
elements.forEach(function(element) {
newSlide.insertPageElement(element);
});
index++;
});
values.forEach(function(page){
if(page[0]){
var landingPage = page[0];
var sessions = page[1];
var newSessions = page[2];
var pagesPer = page[5];
var goalRate = page[7];
var goalValue = page[9];
// Insert slides from template
var index = 0;
templateSlides.forEach(function(slide) {
var newSlide = slides.insertSlide(index);
var elements = slide.getPageElements();
elements.forEach(function(element) {
newSlide.insertPageElement(element);
});
index++;
});
defaultSlides = slides.getSlides(); //update the slides array for
indexes and length
defaultSlide = defaultSlides[1];
newSlide = defaultSlide;
newSlide2 = defaultSlides[2];
var shapes = (newSlide.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{landing page}}',landingPage);
shape.getText().replaceAllText('{{sessions}}',sessions);
shape.getText().replaceAllText('{{new sessions}}',newSessions);
shape.getText().replaceAllText('{{pages per session}}',pagesPer);
shape.getText().replaceAllText('{{goal rate}}',goalRate);
shape.getText().replaceAllText('{{goal value}}',goalValue);
})
var shapes = (newSlide2.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{landing page}}',landingPage);
shape.getText().replaceAllText('{{sessions}}',sessions);
shape.getText().replaceAllText('{{new sessions}}',newSessions);
shape.getText().replaceAllText('{{pages per session}}',pagesPer);
shape.getText().replaceAllText('{{goal rate}}',goalRate);
shape.getText().replaceAllText('{{goal value}}',goalValue);
});
presLength = defaultSlides.length;
newSlide.move(presLength);
newSlide2.move(presLength);
defaultSlides[0].remove();
defaultSlides[3].remove();
} // end our conditional statement
}); //close our loop of values
//Get the charts
var defaultSlides=slides.getSlides();
var defaultSlide = defaultSlides [1]
var position = {right: 490, bottom: 190};
var size = {height: 140, width: 230};
defaultSlide.insertSheetsChart(
chartRegion1,
position.right,
position.bottom,
size.width,
size.height);
var defaultSlides=slides.getSlides();
var defaultSlide = defaultSlides [1]
var position = {right: 200, bottom: 190};
var size = {height: 140, width: 230};
defaultSlide.insertSheetsChart(
chartGender1,
position.right,
position.bottom,
size.width,
size.height);
// Create and display a dialog telling the user where to
// find the new presentation.
var slidesUrl = slides.getUrl();
var html = "<p>Find it in your home Drive folder:</p>"
+ "<p><a href=\"" + slidesUrl + "\" target=\"_blank\">"
+ presentationTitle + "</a></p>";
SpreadsheetApp.getUi().showModalDialog(
HtmlService.createHtmlOutput(html)
.setHeight(120)
.setWidth(350),
"Report Generated!"
);
}
感谢您的帮助。
我相信你的现状和目标如下。
- 您有 Google 个包含一些图表的幻灯片。
- 当图表为“无数据”时,您想从 Google 幻灯片中删除图表。
- 您想使用 Google Apps 脚本实现此目的。
- 从包含示例“无数据”图表的示例电子表格中,您的“无数据”图表没有值。
在这种情况下,下面的示例脚本怎么样?
示例脚本:
请将以下脚本复制并粘贴到包含图表的 Google 电子表格的脚本编辑器中。并且,请将 Google 幻灯片 ID 设置为 presentationId
。
function myFunction() {
const presentationId = "###"; // Please set the Google Slides ID.
// 1. Retrieve the charts with "No data".
const ss = SpreadsheetApp.getActiveSpreadsheet();
const obj = ss.getSheets().reduce((o, sheet) => {
sheet.getCharts().forEach(chart => {
const check = chart.getRanges().some(e => {
const temp = e.getDisplayValues();
return temp[0].map((_, c) => temp.map(r => r[c])).some(col => col.join("") == "");
});
if (check) o[chart.getChartId()] = check;
});
return o;
}, {});
// 2. Remove the charts with "No data".
const slides = SlidesApp.openById(presentationId).getSlides();
slides.forEach(slide => {
slide.getSheetsCharts().forEach(chart => {
if (obj[chart.getChartId()]) chart.remove();
});
});
}
- 当此脚本为运行时,首先在对象中检索“无数据”的图表。并且,使用此对象,删除了 Google 幻灯片中的图表。
注:
- 此示例脚本适用于您的示例电子表格。如果更改“无数据”的条件,则可能无法使用此脚本。所以,请注意这一点。
参考文献:
我正在使用应用脚本将表格中的嵌入图表插入到幻灯片中。但如果 Google 表格上的图表 empty/has 没有数据,我想从幻灯片中删除图表。
我只想在没有数据时从幻灯片中删除图表。但是如果有数据就保留图表
你能帮我添加正确的行来创建这个条件吗?
这是我的图表代码:
function onOpen() {
// Get the Ui object.
var ui = SpreadsheetApp.getUi();
// Create a custom menu.
ui.createMenu('Present Data')
.addItem("Generate Report","generateLandingPagesReport")
.addToUi();
}
function generateLandingPagesReport() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Copy of Overall Performance 1');
var values = sheet.getRange('A2:J23').getValues();
var chartRegion1 = sheet.getCharts()[0];
var chartGender1 = sheet.getCharts()[1];
// Access the template presentation
var templateId = "1bXAYGCKkpZhksXz8gTCgFYbNoI1BIhAZakd68VlXHeo";
var template = SlidesApp.openById(templateId);
var templateSlides = template.getSlides();
// Create a Slides presentation, removing the default
// title slide.
var presentationTitle =
ss.getName() + " Presentation";
var slides = SlidesApp.create(presentationTitle);
var defaultSlides = slides.getSlides();
defaultSlides.forEach(function(slide) {
slide.remove()
});
var defaultSlide = defaultSlides [1];
// Insert slides from template
var index = 0;
templateSlides.forEach(function(slide) {
var newSlide = slides.insertSlide(index);
var elements = slide.getPageElements();
elements.forEach(function(element) {
newSlide.insertPageElement(element);
});
index++;
});
values.forEach(function(page){
if(page[0]){
var landingPage = page[0];
var sessions = page[1];
var newSessions = page[2];
var pagesPer = page[5];
var goalRate = page[7];
var goalValue = page[9];
// Insert slides from template
var index = 0;
templateSlides.forEach(function(slide) {
var newSlide = slides.insertSlide(index);
var elements = slide.getPageElements();
elements.forEach(function(element) {
newSlide.insertPageElement(element);
});
index++;
});
defaultSlides = slides.getSlides(); //update the slides array for
indexes and length
defaultSlide = defaultSlides[1];
newSlide = defaultSlide;
newSlide2 = defaultSlides[2];
var shapes = (newSlide.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{landing page}}',landingPage);
shape.getText().replaceAllText('{{sessions}}',sessions);
shape.getText().replaceAllText('{{new sessions}}',newSessions);
shape.getText().replaceAllText('{{pages per session}}',pagesPer);
shape.getText().replaceAllText('{{goal rate}}',goalRate);
shape.getText().replaceAllText('{{goal value}}',goalValue);
})
var shapes = (newSlide2.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{landing page}}',landingPage);
shape.getText().replaceAllText('{{sessions}}',sessions);
shape.getText().replaceAllText('{{new sessions}}',newSessions);
shape.getText().replaceAllText('{{pages per session}}',pagesPer);
shape.getText().replaceAllText('{{goal rate}}',goalRate);
shape.getText().replaceAllText('{{goal value}}',goalValue);
});
presLength = defaultSlides.length;
newSlide.move(presLength);
newSlide2.move(presLength);
defaultSlides[0].remove();
defaultSlides[3].remove();
} // end our conditional statement
}); //close our loop of values
//Get the charts
var defaultSlides=slides.getSlides();
var defaultSlide = defaultSlides [1]
var position = {right: 490, bottom: 190};
var size = {height: 140, width: 230};
defaultSlide.insertSheetsChart(
chartRegion1,
position.right,
position.bottom,
size.width,
size.height);
var defaultSlides=slides.getSlides();
var defaultSlide = defaultSlides [1]
var position = {right: 200, bottom: 190};
var size = {height: 140, width: 230};
defaultSlide.insertSheetsChart(
chartGender1,
position.right,
position.bottom,
size.width,
size.height);
// Create and display a dialog telling the user where to
// find the new presentation.
var slidesUrl = slides.getUrl();
var html = "<p>Find it in your home Drive folder:</p>"
+ "<p><a href=\"" + slidesUrl + "\" target=\"_blank\">"
+ presentationTitle + "</a></p>";
SpreadsheetApp.getUi().showModalDialog(
HtmlService.createHtmlOutput(html)
.setHeight(120)
.setWidth(350),
"Report Generated!"
);
}
感谢您的帮助。
我相信你的现状和目标如下。
- 您有 Google 个包含一些图表的幻灯片。
- 当图表为“无数据”时,您想从 Google 幻灯片中删除图表。
- 您想使用 Google Apps 脚本实现此目的。
- 从包含示例“无数据”图表的示例电子表格中,您的“无数据”图表没有值。
在这种情况下,下面的示例脚本怎么样?
示例脚本:
请将以下脚本复制并粘贴到包含图表的 Google 电子表格的脚本编辑器中。并且,请将 Google 幻灯片 ID 设置为 presentationId
。
function myFunction() {
const presentationId = "###"; // Please set the Google Slides ID.
// 1. Retrieve the charts with "No data".
const ss = SpreadsheetApp.getActiveSpreadsheet();
const obj = ss.getSheets().reduce((o, sheet) => {
sheet.getCharts().forEach(chart => {
const check = chart.getRanges().some(e => {
const temp = e.getDisplayValues();
return temp[0].map((_, c) => temp.map(r => r[c])).some(col => col.join("") == "");
});
if (check) o[chart.getChartId()] = check;
});
return o;
}, {});
// 2. Remove the charts with "No data".
const slides = SlidesApp.openById(presentationId).getSlides();
slides.forEach(slide => {
slide.getSheetsCharts().forEach(chart => {
if (obj[chart.getChartId()]) chart.remove();
});
});
}
- 当此脚本为运行时,首先在对象中检索“无数据”的图表。并且,使用此对象,删除了 Google 幻灯片中的图表。
注:
- 此示例脚本适用于您的示例电子表格。如果更改“无数据”的条件,则可能无法使用此脚本。所以,请注意这一点。