Google幻灯片API-如何多次复制幻灯片并每次创建唯一的对象ID

朱利安

总的来说,我希望人们将一些数据放入Google表格中,并提供一个按钮,该按钮将获取数据并自动填充预制的Google幻灯片。我选择了一种方法来复制主幻灯片,然后用数据替换单个文本模块。我的目标是否有更好的方法?

这是主幻灯片

主幻灯片

This is the code i use for the duplication, but it is only able to duplicate the master slide once, because the objectId it creates, is always the same: "copiedSlide_001". I have tried to use a random integer for the objectId, but i dont get it to work. Also, i guess the name has to be predictable to be used in the next step. (importing the data into newly copied slide).

function duplicateSlide() {  

    var presentationId = "1XLtOv7FHWwv6R2QEZF_LEhF7twXTlY3IbvsVDKxdDtc";
    var pageId = "g75e5d89173_0_0"
    var requests = [{

      "duplicateObject": {
        "objectId": pageId,
        "objectIds": {
          "g75e5d89173_0_0": "copiedSlide_001",

        }
      }
    } ]Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    }

How do i get the duplication process to work in my favor?

I would appreciate any help highly!

Tanaike
  • You want to copy the master slide several times in Google Slides.
  • You want to give the unique object ID for each copied slide like copiedSlide_001.
    • You are required to know the object ID of the copied slide before copying it.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification point:

  • In this modification, it requests several DuplicateObjectRequest using the method of batchUpdate.

Modified script:

Before you run the script, please enable Slides API at Advanced Google services.

function duplicateSlide() {
  var presentationId = "1XLtOv7FHWwv6R2QEZF_LEhF7twXTlY3IbvsVDKxdDtc"; // Please set the Slides ID.
  var pageId = "g75e5d89173_0_0"; // Please set the page ID of the source slide.
  var newIDs = ["copiedSlide_001", "copiedSlide_002", "copiedSlide_003"]; // Please set the unique ID here.

  var requests = newIDs.reverse().map(function(id) {
    var obj = {};
    obj[pageId] = id;
    return {duplicateObject: {objectId: pageId, objectIds: obj}};
  });
//  requests.push({deleteObject: {objectId: pageId}});
  Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
}
  • When you run the script, as a test case, the source slide is copied 3 times. And each object ID is "copiedSlide_001", "copiedSlide_002" and "copiedSlide_003", respectively.
    • The page IDs of 2nd, 3rd and 4th pages are "copiedSlide_001", "copiedSlide_002" and "copiedSlide_003", respectively.
  • 如果使用requests.push({deleteObject: {objectId: pageId}});,则在复制完成后,将删除第一页(即源幻灯片)。但是我不确定这是否是您想要的结果。因此,我将其作为注释添加到了脚本中。

参考文献:

如果我误解了您的问题,而这不是您想要的方向,我深表歉意。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章