英文:
Google Apps Script - Exception: Page element is not of type shape
问题
I store in a Google Sheets table some data about quotes, which I inject in a Google Slides (duplicate of a template).
My script works replacing the placeholders with the cell content and injecting the images from the URLs. However, I get an error message that breaks the rest of the script.
function createSlide_(currentAppName) {
let presentation = SlidesApp.openByUrl('https://docs.google.com/presentation/d/###/edit');
let templateSlide = presentation.getSlideById('###');
let newSlide = templateSlide.duplicate();
newSlide.setSkipped(false);
newSlide.replaceAllText('{{AppName}}', currentAppName[0]);
newSlide.replaceAllText('{{Company}}', currentAppName[1]);
newSlide.replaceAllText('{{Founder}}', currentAppName[2]);
newSlide.replaceAllText('{{Designation}}', currentAppName[3]);
newSlide.replaceAllText('{{quote}}', currentAppName[4]);
newSlide.getShapes().forEach(s => {
if (s.getText().asString().trim() == "{{AppImage}}") s.replaceWithImage(DriveApp.getFileById(currentAppName[5].match(/[-\w]{25,}/)).getBlob());
if (s.getText().asString().trim() == "{{FounderImage}}") s.replaceWithImage(DriveApp.getFileById(currentAppName[6].match(/[-\w]{25,}/)).getBlob());
});
};
Exception: Page element is not of type shape.
- I also tried to store
var shapes = newSlide.getShapes()
, check that it exists and then iterate iftypeof shapes !== 'undefined'
andshapes.length > 0
. - I also tried the
getPageElements
method - the script does not work.
Any idea please?
英文:
I store in a Google Sheets table some data about quotes, which I inject in a Google Slides (duplicate of a template).
My script works replacing the placeholders with the cell content and injecting the images from the URLs. However, I get an error message that breaks the rest of the script.
function createSlide_(currentAppName) {
let presentation = SlidesApp.openByUrl('https://docs.google.com/presentation/d/###/edit');
let templateSlide = presentation.getSlideById('###');
let newSlide = templateSlide.duplicate();
newSlide.setSkipped(false);
newSlide.replaceAllText('{{AppName}}',currentAppName[0]);
newSlide.replaceAllText('{{Company}}',currentAppName[1]);
newSlide.replaceAllText('{{Founder}}',currentAppName[2]);
newSlide.replaceAllText('{{Designation}}',currentAppName[3]);
newSlide.replaceAllText('{{quote}}',currentAppName[4]);
newSlide.getShapes().forEach(s => {
if (s.getText().asString().trim() == "{{AppImage}}") s.replaceWithImage(DriveApp.getFileById(currentAppName[5].match(/[-\w]{25,}/)).getBlob());
if (s.getText().asString().trim() == "{{FounderImage}}") s.replaceWithImage(DriveApp.getFileById(currentAppName[6].match(/[-\w]{25,}/)).getBlob());
});
};
Exception: Page element is not of type shape.
- I also tried to store
var shapes = newSlide.getShapes()
, check that it exists and then iterate iftypeof shapes !== 'undefined'
andshapes.length > 0
. - I also tried the
getPageElements
method - the script does not work.
Any idea please?
答案1
得分: 1
不确定您的脚本出了什么问题,我创建了一个简单的示例。请尝试这个示例,查看您的模板包含了什么。我创建了一个只有一个文本框的简单幻灯片。
function test() {
try {
let presentation = SlidesApp.getActivePresentation();
let slides = presentation.getSlides();
slides.forEach(slide => console.log(slide.getObjectId()));
let template = presentation.getSlideById("p");
let slide = template.duplicate();
slide.replaceAllText("{{hello}}", "goodbye");
let elements = slide.getPageElements();
elements.forEach(element => console.log(element.getPageElementType().toString()));
}
catch(err) {
console.log(err);
}
}
执行日志
3:52:46 PM Notice Execution started
3:52:47 PM Info p
3:52:47 PM Info SHAPE
3:52:47 PM Notice Execution completed
希望这对您有所帮助。
英文:
Not know exactly what is failing in you script I created a simple example. Try this and see what you template consists of. I have a simple slide with just one text box.
function test() {
try {
let presentation = SlidesApp.getActivePresentation();
let slides = presentation.getSlides();
slides.forEach( slide => console.log(slide.getObjectId()) );
let template = presentation.getSlideById("p");
let slide = template.duplicate();
slide.replaceAllText("{{hello}}","goodbye");
let elements = slide.getPageElements();
elements.forEach( element => console.log(element.getPageElementType().toString()))
}
catch(err) {
console.log(err)
}
}
Execution log
3:52:46 PM Notice Execution started
3:52:47 PM Info p
3:52:47 PM Info SHAPE
3:52:47 PM Notice Execution completed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论