Posts

Sort by Random - How to Randomize the Order of Rows in Google Sheets

Image
You have a workbook in Google Sheets that contains multiple rows of data and you are required to sort the list in a random order. For instance, your sheet may contain the names of your team members and you need to reshuffle the list before assigning tasks to each of the members randomly. Or your Google Sheet may have the email addresses of people who participated in a giveaway and you need to pick any three random entries in an unbiased manner for the prize. There are multiple ways to randomize the data rows in Google Sheet. You can either use the built-in SORT function of Google Sheets or create a menu-based function that lets you randomize data with a click. Demo - Make a copy of this Google Sheet to try random sort with your own data in sheets. Sort Google Sheets in Random Order Open your Google Sheet that contains the list of data and create a new sheet . Paste the following formula in A1 cell of this empty sheet. = SORT ( Customers ! A2 : D50 , RANDARRAY ( ROWS ( Cus...

Exceeded maximum execution time Exception in Google Apps Script

Google Apps Script is a serverless environment that makes it easy for you to work with Gmail, Google Drive and other services that are part of the Google Workspace platform. When you run any code inside the Google Apps Script IDE, in simple English, it spins up a new server with the required environment that are necessary to run your application. This server is allotted a hard timeout limit and the App Script environment will halt the execution of the function if it exceeds the maximum execution time. Exceeded maximum execution time The maximum execution time varies based on the type of your Google Account. If you are running your Apps Script code inside a Gmail account, your functions can run for 6 minutes before it will be terminated. For Google Workspace accounts, because you are paying a monthly fee to Google per user, the timeout limit is more generous at 30 minutes. If your Apps Script function / trigger exceeds the maximum timeout limit, the script will throw an exception ...

How to Replace Accented Characters (diacritics) with English letters in Google Sheets

Image
The REMOVE_ACCENTED function for Google Sheets will replace all accented characters in the referenced cell, like the letters è, õ, ā, ĝ and so on with their normal Latin equivalents. To get started, make a copy of the Google Sheet , go to the Tools menu, choose Script Editor and copy the entire code to your clipboard. Now open your own Google Sheet and paste the same code inside the Script editor of your sheet. Save and you should be able to use the REMOVE_ACCENTED function in your own sheets. Input String Output string A História de Malú e João Miguel A Historia de Malu e Joao Miguel Símbolo de su unidad y permanencia Simbolo de su unidad y permanencia Tomás Gutiérrez Alea Tomas Gutierrez Alea Miguel Ángel Félix Gallardo Miguel Angel Felix Gallardo Internally, this function uses the deburr function of the popular lodash library that converts Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and also removes any combining diacriti...

How to Find and Replace Text in Google Docs with RegEx Search Patterns

It is easy to search and replace text in Google Documents with the DocumentApp service of Google Apps Script. You can use use findText method with simple regular expressions to find text elements in the document that match a pattern and replace them with the specified text. Here’s a simple code sample that replaces the first occurrence of the “GSuite” with “Google Workspace” in the active Google Document. const searchAndReplaceInGoogleDocs = ( ) => { const searchText = "GSuite" ; const replaceText = "Google Workspace" ; const document = DocumentApp . getActiveDocument ( ) ; const documentBody = document . getBody ( ) ; const searchResult = documentBody . findText ( searchText ) ; if ( searchResult !== null ) { const startIndex = searchResult . getStartOffset ( ) ; const endIndex = searchResult . getEndOffsetInclusive ( ) ; const textElement = searchResult . getElement ( ) . asText ( ) ; textEl...

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 ...