There’s a method to make use of Google Docs as a programming IDE and run JavaScript code contained in the editor.
You could have been utilizing Google Docs to put in writing paperwork and essays however do you know that the identical editor may also be used to put in writing and run JavaScript code?
It’s no substitute for a devoted IDE like Visible Studio code however Google Docs can be utilized as a JavaScript playground to rapidly run code snippets.
Right here’s a pattern doc written in Google Docs and the doc physique incorporates a JavaScript perform that calculates the variety of days left till the following Christmas.
Go to the Code Runner
menu, select Run JavaScript
and the output of the perform will show in a popup. See demo
Code Runner in Google Docs
Internally, there’s somewhat Google Apps Script that’s doing the magic. It reads the physique of your Google Doc as a textual content string and makes use of the eval()
perform of JavaScript to judge the textual content.
/**
* @OnlyCurrentDoc
*/
perform codeRunner() {
const doc = DocumentApp.getActiveDocument();
const textual content = doc.getBody().getText();
const response = eval(textual content);
DocumentApp.getUi().alert(response);
}
perform onOpen() {
const ui = DocumentApp.getUi();
const menu = ui.createMenu('Code Runner');
menu.addItem('🦄 Run JavaScript ', 'codeRunner');
menu.addToUi();
}
Associated studying: