Google App脚本页面在光标处中断? [英] Google App Script Page break at cursor?
问题描述
我的侧栏中有一个按钮,其目的是在光标所在的位置插入一个分页符.我尝试编写脚本以仅插入一个分页符(不是在光标处,而是在正文中),并且效果很好.
I have a button in my sidebar of which's purpose is to insert a page break where the cursor is. I tried scripting it to just insert a page break (not at the cursor but just in the body) and it works fine.
但是,我似乎无法使它正常工作:
However, I can't seem to get it to work at the cursor:
function pageBreak() {
var cursor = DocumentApp.getActiveDocument().getOffset();
insertPageBreak(cursor);
}
我该怎么做?
推荐答案
您需要知道光标位置的元素类型.并非所有元素类型都允许插入分页符.
You need to know the element type at the cursor position. Not all element types allow a page break to be inserted.
首先将元素放在光标位置,然后从那里开始工作.
Begin by getting the element at the cursor position, then work from there.
function insertPgBrk() {
var theCursor = DocumentApp.getActiveDocument().getCursor();
var theElement = theCursor.getElement();
var elementTypeAtCursor = theElement.getType();
Logger.log('elementTypeAtCursor: ' + elementTypeAtCursor);
if (elementTypeAtCursor === DocumentApp.ElementType.PARAGRAPH) {
Logger.log('its the right type');
theElement.insertPageBreak(0);
};
};
您不能在TEXT元素中插入分页符.元素必须为PARAGRAPH或LISTITEM.
You can't insert a page break in a TEXT element. The element needs to be a PARAGRAPH or a LISTITEM.
这篇关于Google App脚本页面在光标处中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!