Google App Script Page break at cursor?

£可爱£侵袭症+ 提交于 2019-12-25 05:35:11

问题


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);
}

How can I accomplish this?


回答1:


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);
  };
};

You can't insert a page break in a TEXT element. The element needs to be a PARAGRAPH or a LISTITEM.



来源:https://stackoverflow.com/questions/29736069/google-app-script-page-break-at-cursor

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