Google Docs as Programming IDE

You have been using Google Docs to write documents and essays but did you know that the same editor can also be used to write and run JavaScript code?

It is no replacement for a dedicated IDE like Visual Studio code but Google Docs can be used as a JavaScript playground to quickly run code snippets.

Here’s a sample document written in Google Docs and the document body contains a JavaScript function that calculates the number of days left until the next Christmas.

Go to the Code Runner menu, choose Run JavaScript and the output of the function will display in a popup. See demo

Google Docs - Code Runner

Code Runner in Google Docs

Internally, there’s a little Google Apps Script that is doing the magic. It reads the body of your Google Document as a text string and uses the eval() function of JavaScript to evaluate the text.

/**
 * @OnlyCurrentDoc
 */

function codeRunner() {
  const doc = DocumentApp.getActiveDocument();
  const text = doc.getBody().getText();
  const response = eval(text);
  DocumentApp.getUi().alert(response);
}

function onOpen() {
  const ui = DocumentApp.getUi();
  const menu = ui.createMenu("Code Runner");
  menu.addItem("🦄 Run JavaScript ", "codeRunner");
  menu.addToUi();
}

Related reading:



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Author

prakhar@affmantra.com

Related Posts

How to Handle OAuth Permissions in Google Add-ons

Table of Contents 1. How to Check for Required OAuth Scopes 1.1 The “Authorization Catch-22” Problem 1.2 How to Reset the Permissions...

Read out all

How to Recover Permanently Deleted Files and Folders in Google Drive

Table of Contents When you delete any file or folder in your Google Drive, it is moved to the trash folder. The...

Read out all

Simple URL Tricks for Google Drive You Should Know

Table of Contents 1. Google Drive URL Tricks 1.1 Google Drive Web Viewer 1.2 Reader Mode for Google Drive Files 1.3 Embed...

Read out all

How to Extract URLs from HYPERLINK Function in Google Sheets

The HYPERLINK formula of Google Sheets lets you insert hyperlinks into your spreadsheets. The function takes two arguments: The full URL of...

Read out all

Find and Remove Inactive Users in your Google Workspace Domain

Table of Contents 1. Find the inactive users in Google Workspace domain You can use Google Apps Script to find all the...

Read out all

The Best Online Tools To Know Everything About a Website

The Best Online Tools To Know Everything About a Website Source link

Read out all