admin管理员组

文章数量:1295154

I need to be able to change a string of text to heading1 and do some additional formatting later on. Caught up on this part for now. Here is my sample code as of right now.

function pocket() {
    // Try to get the current selection in the document. If this fails (e.g.,
    // because nothing is selected), show an alert and exit the function.
    var selection = DocumentApp.getActiveDocument().getSelection();
    var body = DocumentApp.getActiveDocument().getBody();
    if (!selection) {
        DocumentApp.getUi().alert('Cannot find a selection in the document.');
        return;
    }
    var selectedElements = selection.getSelectedElements();
    var selectedElement = selectedElements[0];
    var pocketelement = selectedElement.getElement().getText();
    body.appendParagraph(pocketelement).setHeading(DocumentApp.ParagraphHeading.HEADING1);
}

I need it to delete the original string and append the paragraph at the original strings location. Not sure how to do that. any help will be appreciated!

I need to be able to change a string of text to heading1 and do some additional formatting later on. Caught up on this part for now. Here is my sample code as of right now.

function pocket() {
    // Try to get the current selection in the document. If this fails (e.g.,
    // because nothing is selected), show an alert and exit the function.
    var selection = DocumentApp.getActiveDocument().getSelection();
    var body = DocumentApp.getActiveDocument().getBody();
    if (!selection) {
        DocumentApp.getUi().alert('Cannot find a selection in the document.');
        return;
    }
    var selectedElements = selection.getSelectedElements();
    var selectedElement = selectedElements[0];
    var pocketelement = selectedElement.getElement().getText();
    body.appendParagraph(pocketelement).setHeading(DocumentApp.ParagraphHeading.HEADING1);
}

I need it to delete the original string and append the paragraph at the original strings location. Not sure how to do that. any help will be appreciated!

Share Improve this question edited Oct 17, 2017 at 3:06 Wicket 38.4k9 gold badges78 silver badges192 bronze badges asked Dec 17, 2014 at 1:34 tmoney226tmoney226 1052 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

The body.appendParagraph() method appends the given paragraph, like javascript, to the body, at the end of it.

You are looking for the .insertParagraph(index, Paragraph). To get the index, try body.getChildIndex(paragraph). See the code below.

function pocket() {
  // Try to get the current selection in the document. If this fails (e.g.,
  // because nothing is selected), show an alert and exit the function.
  var doc = DocumentApp.getActiveDocument();
  var selection = doc.getSelection();
  var body = doc.getBody();
  if (!selection) {
      DocumentApp.getUi().alert('Cannot find a selection in the document.');
      return;
  }
  var selectedElements = selection.getSelectedElements();
  var selectedElement = selectedElements[0];
  //holds the paragraph
  var paragraph = selectedElement.getElement();
  //get the index of the paragraph in the body
  var paragraphIndex = body.getChildIndex(paragraph);
  //remove the paragraph from the document
  //to use the insertParagraph() method the paragraph to be inserted must be detached from the doc
  paragraph.removeFromParent();
  body.insertParagraph(paragraphIndex, paragraph).setHeading(DocumentApp.ParagraphHeading.HEADING1);
};

本文标签: javascriptAppending Paragraph at specific locationStack Overflow