Google doc as http rest api using Apps Script: Json

JRichardsz.java
4 min readMar 14, 2021

--

On my previous post about google doc to api rest, I don’t know what I was think to use xml o_O

https://medium.com/@jrichardsz.java/google-doc-as-rest-api-using-apps-script-dd83e6b1522e

Sorry for that. Now inspired on another of my creations:

In which I was able to parse a raw text like this into a object:

I will use a same algorithm to transform a google docs text into a json.

Step 1: Create a google docs like this

name: AbogadoJC

phone: +511 112358

address: 456 Street, Orlando, USA

time: 10 AM : 1PM

mail: foo@gmail.com

message_3_title: At vero eos et accusamus et iusto

Step 2: Add an app script

In the text editor paste this code https://gist.githubusercontent.com/jrichardsz/b347036f14e8ad76abd5de7bbe8e92c1/raw/7f46eac9bf6156910ed9c267dac078ee80f8334c/google%2520doc%2520as%2520api%2520rest-1.0.2.js

Press the “save” icon and set some name

Return to your google doc and copy the document id. Is the part of the url after d/ and before /edit

Paste that value at the beggining of javascript

To test, select this function and press run

The copied javascript code read the google docs, get the raw text, and convert it to a json. If you worry about that, review the code. If you validated that is a trusted javascript, accept the warning permissions:

Choose your mail, press advanced and go to (unsafe)

Press allow

If everything is fine, the log should look like this:

Step 3: Deploy the app script as http public service

Select deploy and New deployment

Select type : web app

Enter a description, choose “Anyone” and deploy

That’s all, you will see an url which is ready to use

Url should be like this

https://script.google.com/macros/s/AKfycb***AQIGQ/exec

Using postman or any http client, you could perform a get operation and you will see the json:

How it works?

Using this function, an app script could get the text of any google docs of the gmail account

function getFileText(idDocFile) {
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/"+idDocFile+"/edit");
var text = doc.getBody().getText();
return text;
}

After that using this algorithm , the raw text is converted to a json string

function getJSonFromRawText(rawText){
var lines = rawText.split('\n');
if(typeof lines === 'undefined' || lines.length == 0){
return {};
}
var firstLevelObjectAttributes = {};

//todo: what if the value is multiline?
for(var i = 0;i < lines.length;i++){

var trimmedLine = lines[i].trim();
if(trimmedLine.length == 0){
continue;
}

log(trimmedLine);
var keyMatch = trimmedLine.match(/^\s*\w+:/)
if(typeof keyMatch === 'undefined'){
continue;
}
var key = keyMatch[0].trim().slice(0,-1);
log(key);

var valueMatch = trimmedLine.match(/:.+/)
if(typeof valueMatch === 'undefined'){
continue;
}
var value = valueMatch[0].substring(1).trim();
log(value);

firstLevelObjectAttributes[key] = value;
}
return JSON.stringify(firstLevelObjectAttributes);
}

Finally using this function we expose the generated json as http public resource which returns a valid json

function doGet(request) {
try {
var json = getGoogleDocAsJson();
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
} catch (err) {
log(err);
return ContentService.createTextOutput(JSON.stringify(err)).setMimeType(ContentService.MimeType.JSON);
}
}

Conclusion

Google apps script is a powerful feature far superior to Microsott Excel Macros. With this javascript we could do awesome things like expose the content of google doc as json

This will work very well for landing pages, proof of concepts, demos, etc

Check this to expose a google sheet as api

--

--

JRichardsz.java

Programmer born in vulcan who searches for his internal properties file to perform an overclock in his brain and body. https://stackoverflow.com/users/3957754