Create the objectIds map with variable keys for a DuplicateObject request

北慕城南 提交于 2019-11-28 08:46:19

问题


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 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.

How can I assign variable keys to the objectIds map?


回答1:


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 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'}

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'}

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

References:

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


来源:https://stackoverflow.com/questions/51728567/create-the-objectids-map-with-variable-keys-for-a-duplicateobject-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!