Google Apps Script – 异常:页面元素不是形状类型

huangapple go评论69阅读模式
英文:

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).

Google Apps Script – 异常:页面元素不是形状类型

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 if typeof shapes !== 'undefined' and shapes.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).

Google Apps Script – 异常:页面元素不是形状类型

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 if typeof shapes !== 'undefined' and shapes.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

huangapple
  • 本文由 发表于 2023年2月9日 02:26:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定