Posts

Showing posts from June, 2021

How to Change the Font in your Google Documents with Apps Script

An organization recently migrated their Word Documents from Microsoft Office to Google Drive. The migration has been smooth but the Word documents imported as Google Docs are using Calibri, the default font family of Microsoft Word. The company is looking to replace the fonts in multiple Google Documents such that the document headings using Georgia while the body paragraphs are rendered in Droid Sans at 12 pt. Replace Font Styles in Google Docs This example show how to replace the font family of your Google Documents of specific sections - the heading titles are rendered in a different font while the tables, list items, body and table of contents are formatted with a separate font. const updateFontFamily = ( ) => { const document = DocumentApp . getActiveDocument ( ) ; const headingStyles = { [ DocumentApp . Attribute . FONT_FAMILY ] : "Georgia" , [ DocumentApp . Attribute . FONT_SIZE ] : 14 , } ; const normalParagraphStyles = {

How to Replace Text and Hyperlinks in Google Documents with Apps Script

The company’s handbook is written in Google Docs. The document spans several pages and now the writer has been asked to create links such that all mentions of the company name in the document are linking to the company’s official website. It can be a time consuming task but with Google Apps Script, specific words in a document can be hyperlinked in bulk in one click. Add Hyperlinks in Google Docs This example show how to search and replace all occurrences of a text phrase, the company name in this case, and add links to a specific website. const addLinks = ( ) => { const searchPhrase = "Digital Inspiration" ; const hyperlink = "https://digitalinspiration.com/" ; const document = DocumentApp . getActiveDocument ( ) ; const body = document . getBody ( ) ; let search = null ; while ( ( search = body . findText ( searchPhrase , search ) ) ) { const searchElement = search . getElement ( ) ; const startIndex =

Google Documents - How to Replace Text in Header and Footer

Image
The upcoming release of Document Studio includes support for adding markers in the header, footer and the footnotes section of your Microsoft Word template. The add-on will automatically replace this placeholder text with actual values sourced from Google Sheets or Google Forms. Replace Header and Footer with Document API This Apps Script snippet uses the Google Docs API to find and replace multiple blocks of text in the header and footer section of your Google Document. The header and footer sections are children of the parent DOCUMENT section. const replaceHeaderFooter = ( ) => { // Returns the document with the specified ID const doc = DocumentApp . openById ( "DOCUMENT ID" ) ; // Retrieves the headers's container element which is DOCUMENT const parent = doc . getHeader ( ) . getParent ( ) ; for ( let i = 0 ; i < parent . getNumChildren ( ) ; i += 1 ) { // Retrieves the child element at the specified child index

How to Convert Column Numbers (e.g. 28) to A1 Notation (e.g. AB) in Google Sheets

Image
Google Sheets includes built-in functions for converting cell references in A1 notation to row and column numbers and another function for converting column alphabets (like AA) into the column index (26 in this case). =ADDRESS(23, 28, 4) - Returns the A1 style notation of the cell whose row number is 23 and column number is 28. =COLUMN(C9) - Returns the column number of a specified cell C9 where column A corresponds to 1 and column AA corresponds to 27. Get A1 Notation with JavaScript If you are working with the Google Sheets API, you may sometimes needs to calculate the A1 notation style reference of a cell whose row and column numbers are known in the JSON data of the sheet. For container bound Google Sheets, the getA1Notation() method can return the range address in A1 Notation. const sheet = SpreadsheetApp . getActiveSheet ( ) ; const range = sheet . getRange ( 1 , 2 ) ; Logger . log ( range . getA1Notation ( ) ) ; If you are not using the Spreadsheet service,