使用DuplicateObject请求的可变键创建objectIds映射 [英] Create the objectIds map with variable keys for a DuplicateObject request

查看:93
本文介绍了使用DuplicateObject请求的可变键创建objectIds映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建重复对象"请求时,objectIds映射不允许我在下面的强调部分中放置变量:

When creating a "duplicate object" request, the objectIds map will not let me put a variable in the emphasized portion below:

var alternateSlideId = 'alternate_' + i;
{ 
    duplicateObject:
    {
        objectId: alternateSlide,
        objectIds: {
            **alternateSlide**: 'Copied_Alternate_Slide_' + i,
        }
    }
}

(i是一个循环数字) 据我了解,map函数的作用类似于
((objectId to be copied) : (name of new objectId))
我无法在map函数的左侧使用变量,也无法将'alternate_' + i放入地图的左侧,并且不确定原因.我需要复制以前已经复制过的多张幻灯片,因此要有变量名.

(i is a number in a loop) From my understanding the map function works like
((objectId to be copied) : (name of new objectId))
I'm unable to use a variable on the left side of the map function, and I'm unable to put 'alternate_' + i into the left side of the map and I'm uncertain as to why. I need to duplicate multiple slides that have already been duplicated before, and thus have variable names.

如何为objectIds映射分配变量键?

How can I assign variable keys to the objectIds map?

推荐答案

您需要指定唯一 ID.不仅是幻灯片特有的,而且整个演示文稿也是如此.考虑像在此答案中一样使用Utilities.getUuid().

You need to specify unique ids. Not just unique to the slide, but the whole presentation. Consider using Utilities.getUuid() as I do in this answer.

Google Apps脚本本质上是JavaScript 1.6,因此要写入变量属性名称,您需要使用括号运算符,而不是对象文字构造函数中的点运算符或变量名称/简写语法.您过去的尝试很可能试图从构造函数中做到这一点:

Google Apps Script is essentially JavaScript 1.6, so to write to a a variable property name, you need to use the bracket operator, rather than the dot operator or the variable name / shorthand syntax in the object literal constructor. Your past attempts likely attempted to do this from the constructor:

不起作用(对象文字构造函数):

Won't work (object literal constructor):

var v = "some prop name";
var myObj = {
  v: "some prop value",
};
console.log(myObj); // {'v': 'some prop value'}

不起作用(点运算符):

Won't work (dot operator):

var v = "some prop name";
var myObj = {};
myObj.v = "some prop value";
console.log(myObj); // {'v': 'some prop value'}

不起作用(因为GAS不是ECMAScript2015或更高版本),并且在保存时抛出无效的属性ID"错误:

Won't work (since GAS is not ECMAScript2015 or newer), and throws "Invalid property ID" error when saving:

var v = "some prop name";
var myObj = {
  [v]: "some prop value",
};
console.log(myObj);

会工作(括号运算符):

Will work (bracket operator):

var v = "some prop name";
var myObj = {};
myObj[v] = "some prop value";
console.log(myObj); // {'some prop name': 'some prop value'}

因此,您要复制由变量altSlide表示的幻灯片的代码必须类似于:

Thus, your code to copy the a slide represented by the variable altSlide needs to be something like:

var newAltSlideId = ("copied_altSlide_" + i + "_" + Utilities.getUuid()).slice(0, 50);
var dupRqConfig = {
  objectId: altSlide.objectId, // the object id of the source slide
  objectIds: {} // map between existing objectIds on the altSlide, and the new ids
};
// Set the duplicate's ID
dupRqConfig.objectIds[altSlide.objectId] = newAltSlideId;
// If you want to set the objectIds in the duplicate, you need to
// loop over altSlide's child objects. An example:
altSlide.pageElements.forEach(function (child, index) {
  dupRqConfig.objectIds[child.objectId] = /** some new id */;
});
requests.push({ duplicateObject: dupRqConfig }); // add the request to the batchUpdate list

参考:

  • Object initializer
  • DuplicateObject Request
  • Page resource (aka a "slide")
  • "Copy a slide" sample
  • Array#forEach

这篇关于使用DuplicateObject请求的可变键创建objectIds映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆