I use a template in google docs for a letter and I want to fill the letter after the (dynamic) header of the letter automatically via App Scripts. Is there any best practice to set the text always to the same position? I've tried to use a placeholder and replace it. But sometimes I need to replace the text more than one time.
Best practice for filling a text range in Google Docs App Script
601 views Asked by mm1975 At
2
There are 2 answers
0
TheWizEd
On
I don't know about best practice but you could use bookmarks. You can't name them but if you know, lets say, the 5th bookmark is user name then cycle through the bookmarks and when you get to the 5th edit the text.
Before:
function myFunction() {
try {
var doc = DocumentApp.getActiveDocument();
var bmarks = doc.getBookmarks();
var position = null;
var i = 0;
for( i=0; i<bmarks.length; i++ ) {
console.log(bmarks[i].getId());
position = bmarks[i].getPosition();
position.insertText("i = "+i)
}
}
catch(err) {
console.log(err);
}
}
After:
Related Questions in GOOGLE-APPS-SCRIPT
- How to use custom font during html to pdf conversion?
- parameter values only being sent to certain columns in google sheet?
- AppScript to replace image in google slides
- Email Sent Using AppScript Appears in Sent Box, but does not appear in Recipient's Inbox
- Update a MySQL row depending on the ID in Google Sheets Apps Script
- Would like the script to return the PDF letter via email with the sheet name not the spreadsheet name
- App script to prevent duplicate form submission
- Redirect Users to another website using GAS
- How do I create a Line Graph on App Script?
- Error (Status of 500) and X-Frame-Options with google apps script redirect function
- How do I add the Luxon library google sheet script
- AppScript For Google Form
- google sheet script to convert the Scientific notation in the selected area to number
- onEdit() 'row is not defined' error using checkboxes
- Regex expression not working on Google Form
Related Questions in GOOGLE-DOCS
- Keeping image and text together on one page in Google Docs using Google Apps Script
- Turn off table row overflow across pages
- Cannot get the content of the docx file by the export link
- Remove placeholder text from cell and merge in google docs document using MergeTableCellsRequest
- Can I Have a Filter Dynamically Update on Google Sheets?
- I have this script where I am trying to call up 2 different functions using the menu items I created, one function keeps giving me a reference error
- Google Docs Embedded document giving errors
- How to fix 'Script function not found' error in Google Apps Script?
- Javascript Simulating Input in Google Docs "Find In Document" Prompt
- Replacing Image in Google Doc with Python
- Apps script to append doc pages for mass direct mail
- Open all hyperlinks in a Google Doc in a separate browser window
- Chrome Extension: Manipulating Google Docs DOM to Adjust Companion Sidebar
- How to use google.script.run to call a server side function on drop down change? (Google Docs)
- How to use page tokens to export a larger number of comments on a Google Doc to a Google Sheet
Related Questions in NAMED-RANGES
- Power Query / M Code, extract a list of tables into one main table, some column headers same but some different and in different order (and in row 2)
- How to pass variable string as an argument to Names Object
- ArrayFormula with SUMIFS not Working in Google Sheets
- Excel VBA Named Range Scope and/or Permanence of a Named Range
- How to Use a Dynamic Named Range Variable in Power Query Table.PIvot Function for Automation
- 2 criteria named range Excel
- Export R dataframe to Excel named range
- Runtime error 1004, method range of object _worksheet failed. Defining ranges in VBA
- Excel replace Indirect function with a non volatile formula but referencing a cell with named ranges in
- Range of Object Failure
- Why FIND returns nothing with structured table?
- Google Sheets - Return Cell Name
- Using the IF statement formula below, can I make any bank holiday dates regardless of time to theatre show as "out of hours" ? using a named range
- Deleting named ranges from active cells in google sheets with a script
- How do I fix a run-time error 13 mismatch on a range value?
Related Questions in TEXTRANGE
- What does the range.expand() function do in JS?
- VBA webbrowser findtext next occurrences
- VBA, setting a range in a Word document to edit a specific section of text
- Best practice for filling a text range in Google Docs App Script
- Changing parts of the text font and color in PowerPoint Add-In in C#
- Get TextRange of the current word at caret
- if else and while in M language
- WPF equivalent to Windows Forms selection of length 0
- Change TextRange font color, but not the text that will be written next to it
- Is it possible to get text style for each words individually?
- How do I set a property to the last typed symbol in richtextbox in wpf?
- How to append a TextRange to flow document without losing formatting
- TextRange.Find().Delete method is deleting leading/trailing spaces in a textbox
- run time error -2147024809 (80070057) the specified value is out of range with shape
- Powerpoint VBA foreach skipping some valid shapes
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)


In your situation, how about using NamedRange? Ref
When the NamedRange is used, the script is as follows.
Sample script:
Usage:
setNamedRange. By this, the point of the current cursor is set as the named range.changeTextByNamedRange. By this, the updated text is put to the named range as the replacement.When you want to delete the named range, please run
deleteNamedRange.Testing:
When the above script is run, the following result is obtained.
Reference: