Stay organized with collections Save and categorize content based on your preferences.
PositionA reference to a location in the document tab, relative to a specific element. The user's cursor is represented as a Position
, among other uses. Scripts can only access the cursor of the user who is running the script, and only if the script is bound to the document.
// Insert some text at the cursor position and make it bold. const cursor = DocumentApp.getActiveDocument().getCursor(); if (cursor) { // Attempt to insert text at the cursor position. If the insertion returns // null, the cursor's containing element doesn't allow insertions, so show the // user an error message. const element = cursor.insertText('ಠ‿ಠ'); if (element) { element.setBold(true); } else { DocumentApp.getUi().alert('Cannot insert text here.'); } } else { DocumentApp.getUi().alert('Cannot find a cursor.'); }Detailed documentation
getElement()
Gets the element that contains this Position
. This will be either a Text
element or a container element like Paragraph
. In either case, the relative position within the element can be determined with getOffset()
.
Element
— the container or Text
element in which this Position
object is located
getOffset()
Gets this Position
's relative location within the element that contains it. If the element is a Text
element, the offset is the number of characters before the Position
(that is, the index of the character after this Position
); for any other element, the offset is the number of child elements before this Position
within the same container element (that is, the index of the child element after the Position
).
Integer
— for Text
elements, the number of characters before this Position
; for other elements, the number of child elements before this Position
within the same container element
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
getSurroundingText()
Creates an artificial Text
element that represents the text and formatting of the Paragraph
or ListItem
that contains the Position
, either directly or through a chain of child elements. To determine the Position
's offset in the returned Text
element, use getSurroundingTextOffset()
.
Text
— an element equivalent to the result of calling editAsText()
on the Paragraph
or ListItem
that contains the Position
, either directly or through a chain of child elements
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
getSurroundingTextOffset()
Gets the offset of this Position
within the Text
element returned by getSurroundingText()
. The offset is the number of characters before the Position
(that is, the index of the character after this Position
).
Integer
— the number of characters before this Position
in the Paragraph
or ListItem
that contains the Position
, either directly or through a chain of child elements
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
insertBookmark()
Creates and inserts a new Bookmark
at this Position
.
Bookmark
— the new bookmark
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
insertInlineImage(image)
Creates and inserts a new InlineImage
at this Position
from the specified image blob.
image
BlobSource
the image data to insert at this Position
Return
InlineImage
— the new image element, or null
if the element in which this Position
is located does not allow images to be inserted
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
insertText(text)
Inserts the specified text at this Position
. This method creates a new Text
element, even if the string is inserted within an existing Text
element, so that it is easy to style the new element.
text
String
the string to insert at this Position
Return
Text
— the new text element, or null
if the element in which this Position
is located does not allow text to be inserted
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-12-03 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-03 UTC."],[[["A `Position` in Google Docs refers to a location within a document, often representing the user's cursor, and can be used to insert or manipulate content."],["The `Position` class provides methods to get its location within an element, insert text, images, or bookmarks, and access the surrounding text."],["When using `Position` methods to manipulate a document, ensure your script has the necessary authorization scopes, such as `https://www.googleapis.com/auth/documents`."],["Several `Position` methods, like `insertText` and `insertInlineImage`, return a new element representing the inserted content, allowing for further manipulation like styling."],["If a `Position` is within an element that doesn't allow insertions, methods like `insertText` will return `null`, and you may need to handle this case in your script."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4