Retrieving Reformatted Scripts without Comments in a Project using Google Apps Script

Gists

Overview

This is a sample script for easily retrieving the reformatted scripts without comments in a project using Google Apps Script (GAS).

Description

When I create GAS script, if the format of script is not correct, the script editor lets me know about it. By this, I can find that the script editor and/or Google Drive checks the format of scripts. I had wished if I could use this function. Recently, I noticed an interesting function. A GAS project is created and when function myFunction() {Logger.log(this)} is run in the script, I noticed that all scripts in the project are included in this. Furthermore, when I saw the retrieved script, also I noticed that their scripts are reformatted and all comments are removed. In the case of Apps Script API, when the scripts are retrieved by the API, the retrieved script is the same to the original one. So I think that this will help users retrieve simply the reformatted GAS scripts, and such the reformatted scripts will be able to be used for the various situations.

If this will be useful for the developers, I’m glad. And if this information has already been reported, I’m sorry.

Usage

  • Please put this script to the created script.
    • You can use this for the standalone type and the container-bound script type.
  • Run getScripts().
    • In this script, the all scripts except for getScripts() are retrieved from the project, and save it as a text file.

this includes only GAS scripts. HTML and json files are not included.

function getScripts() {
  var scripts = Object.keys(this).map(function(e){return typeof this[e] == "function" && e != "getScripts" ? this[e] : ""}).filter(String).reverse();
  DriveApp.createFile("reformattedScripts.txt", scripts.join(""), MimeType.PLAIN_TEXT);
}

Results

As a sample, it supposes that the following scripts are in a project. These functions includes some comments. And also although there are no error as a script, the indent is no good.

function getScripts() {
  var scripts = Object.keys(this).map(function(e){return typeof this[e] == "function" && e != "getScripts" ? this[e] : ""}).filter(String).reverse();
  DriveApp.createFile("reformattedScripts.txt", scripts.join(""), MimeType.PLAIN_TEXT);
}

function myFunction1() {
  // sample comment 1
}

function myFunction2() {
  Logger.log("sample") // sample comment 2
}

// sample comment 3
function myFunction3() {for(var i=0;i<10;i++){for(var j=0;j<10;j++){Logger.log(i+j)}}}

When getScripts() is run, the following scripts can be retrieved. You can see the reformatted scripts without comments.

function myFunction1() {
}

function myFunction2() {
    Logger.log("sample");
}

function myFunction3() {
    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            Logger.log(i + j);
        }
    }
}

 Share!