我想创建一个具有自定义样式的新 Table 元素,并将其附加到文档中的当前光标位置。我发现可以通过以下方式获取当前位置:
var cursor = DocumentApp.getActiveDocument().getCursor();
返回一个 Position 对象。 Position 类提供了一种将 Text 附加到当前位置的方法:
cursor.insertText('ಠ‿ಠ');
但从文档 https://developers.google.com/apps-script/reference/document/position我看不到插入通用元素(在我的例子中是表)而不是简单文本的方法。
另一方面,Body 类有一个方法,允许将 Table 附加到文档末尾,但无法指定自定义位置。
有人可以帮忙吗?
Thanks
这是我将其拼凑在一起的方法。我有一个函数createComponentTable
这需要一个布尔值atCursor
,如果为 false,则在文档末尾追加一个表格;如果为 true,则在光标位置插入一个表格。
(我也有一个功能doAlert(text)
我可以在其他地方打开/关闭调试。)
function createComponentTable(atCursor) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var thisTable;
if (atCursor) {
var cursor = doc.getCursor();
if (cursor) {
var element = cursor.getElement();
if (element) {
var parent = element.getParent();
thisTable = body.insertTable(parent.getChildIndex(element) + 1);
} else {
doAlert('Cannot insert there');
}
} else {
doAlert('Could not get cursor position');
}
} else {
thisTable = body.appendTable();
}
if (thisTable) {
// add rows and cells to thisTable here
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)