如何使用Google- app - script为Google幻灯片内容分配自定义属性



我希望某些图像有一个额外的属性。基本上,我的目标是唯一地识别我使用App Script代码添加的图像,而不是在Google Slides应用程序中。这是我目前看到的

const dataBlob = Utilities.newBlob(dataBaseEight);
const presentation = SlidesApp.openByUrl('https://docs.google.com/presentation...');
const slides = presentation.getSlides();
const x = slides[0].insertImage(dataBlob);
x.isAddedUsingAppScript = true;
Logger.log(x.isAddedUsingAppScript);//true
Logger.log(slides[0].getImages()[0].isAddedUsingAppScript);//false

供参考,在测试代码之前,我总是确保第一张幻灯片中没有图像。

查看图像类,没有方法可以为图像添加自定义属性,但是您有一些变通方法。

insertImage(blobSource)返回插入的图像,因此您可以在其上使用setDescription()来添加真/假值,并稍后使用getDescription()调用它:

const dataBlob = Utilities.newBlob(dataBaseEight);
const presentation = SlidesApp.openByUrl('https://docs.google.com/presentation...');
const slides = presentation.getSlides();
const x = slides[0].insertImage(dataBlob);
x.setDescription("true")
Logger.log(slides[0].getImages()[0].getDescription()) //returns "true"

这样做的问题是"描述"字段也可以通过右键单击图像并选择"Alt text"进行编辑,因此用户可能会混淆它,或者您可能还想将其用于其他内容。

另一种识别图像的方法是通过它们的getObjectID()方法,该方法返回唯一的ID。您可以使用Properties Service获取这些唯一的id并存储它们的true/false值:

const dataBlob = Utilities.newBlob(dataBaseEight);
const presentation = SlidesApp.openByUrl('https://docs.google.com/presentation...');
const slides = presentation.getSlides();
const x = slides[0].insertImage(dataBlob);
var id = x.getObjectID() // returns "some-id"
var props = PropertiesService.getDocumentProperties() // call the properties service
props.setProperty(id, true) // set "true" to the some-id key
Logger.log(slides[0].getImages()[0].getObjectID()) //returns "some-id" again
Logger.log(props.getProperty("some-id")) // returns "true"

这不会耗尽描述字段,并且用户更难以意外更改值,但是您可能必须更密切地跟踪图像及其保存的属性,因为即使图像被删除,属性也会保留。

请注意,在这两种情况下保存的值都是string,而不是boolean

引用

<
  • 类图像/gh>服务属性>

最新更新