[英]How to assign custom properties to Google Slides contents using Google-App-Script
I would like certain images have an extra property.我希望某些图像具有额外的属性。 Essentially my goal is to uniquely identify the images i've added using App Script code and not in the Google Slides app.
本质上,我的目标是唯一标识我使用 App Script 代码添加的图像,而不是在 Google Slides 应用程序中添加的图像。 This is what I have so far
这是我到目前为止所拥有的
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
FYI I always make sure to have no images in the first slide prior to testing the code.仅供参考,在测试代码之前,我总是确保第一张幻灯片中没有图像。
Looking at the Image Class , there are no methods to add custom attributes to the images, but you have a couple workarounds.查看图像 Class ,没有向图像添加自定义属性的方法,但您有几个解决方法。
insertImage(blobSource)
returns the image that was inserted, so you can use setDescription()
on it to add your true/false value and call it later with getDescription()
: 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"
The problem with this is that the "Description" field is also editable by right-clicking the image and selecting "Alt text", so users could mess with it or you may also want to use it for something else.这样做的问题是,“描述”字段也可以通过右键单击图像并选择“替代文本”来编辑,因此用户可能会弄乱它,或者您可能还想将它用于其他用途。
Another way to identify the images is by their getObjectID()
method which returns a unique ID.识别图像的另一种方法是通过返回唯一 ID 的
getObjectID()
方法。 You can get these unique IDs and store using the the Properties Service along with their true/false value:您可以使用 属性服务获取这些唯一 ID 并存储它们及其真/假值:
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"
This doesn't use up the description field and it's harder for users to accidentally change the values, but you may have to keep closer track of the images and their saved properties since the properties will remain even if the images are deleted.这不会用完描述字段,并且用户更难意外更改值,但您可能必须密切跟踪图像及其保存的属性,因为即使图像被删除,属性仍将保留。
Do note that in both cases the saved values are string
, not boolean
.请注意,在这两种情况下,保存的值都是
string
,而不是boolean
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.