主要用于切割和粘贴元素: 这些都是有效的命令,
{
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var N = [some number]//the element to copy
var elementVar = body.getChild(N)
if (element.getType = 'LIST_ITEM')
{
body.appendListItem(elementVar.copy());//copy is required
}
//replace XXX_XXX below with one of about a dozen available types.
if (element.getType = 'XXX_XXX')
{
body.appendXxxXxx(elementVar.copy());
}
//etc.
}
但是如果你想在不知道它的类型的情况下插入一个元素并且没有使用一堆if语句呢?
{
...
body.appendElement(elementVar.copy());//error
}
答案 0 :(得分:0)
考虑事先在对象中映射出可能的函数......
var map = {
'LIST_ITEM' : function(copy) { body.appendListItem(copy); },
'PARAGRAPH' : function(copy) { body.appendParagraph(copy); }
};
var child = body.getChild(N);
var copy = child.copy();
// pass copy to corresponding append func based on type
map[child.getType()](copy);