简体   繁体   English

如何使用 Google-App-Script 将自定义属性分配给 Google Slides 内容

[英]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

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何结合两个功能google-app-script? - How to combine two functions google-app-script? google-app-script 如何将所有 blob 合并为一个 blob - google-app-script how to cocat all blob into one blob 如何使用JDBC和Google-App-Script对多个查询保持连接状态? - How can I keep my connection alive for multiple queries using JDBC and Google-App-Script? 如何使用谷歌应用脚本将谷歌幻灯片转换为带有注释的 pdf? - how to convert google slides to pdf with notes using Google app script? 有没有办法在多个不同的工作簿中使用相同的google-app-script? - Is there a way to use the same google-app-script in multiple different workbooks? 通过google-app-script创建DCM报告 - creating DCM report through google-app-script Google-App-Script vs PHP编码为base64 - Google-App-Script vs php in encoding base64 google-app-script 暂停键控输入/箭头键 - google-app-script Pause for keyoared input/Arrowkey Google-app-script:使用电子表格中的单元格行[]的JavaScript电子邮件正文中的日期格式问题 - Google-app-script: Date formating issue in a javascript email body using a cell row[] in Spreadsheet 在我的情况下,如何自动运行google-app-script? 设置触发器就足够了吗? - How to automatically run a google-app-script in my case? Is setting a trigger is enough?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM