Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an imported module inside Google App Script

I am trying to use string similarity inside Google App Script, however it is not entirely clear to me how to get it working inside App Script, I get multiple errors, such as "require" is not defined, as another note, the module itself appears to have a dependency.

My final goal is to use this script to match string score between one array with strings full of typos to one with correct strings all within App Script. This is my failed code.

function matchEmails() {   var doc =
 SpreadsheetApp.openById(("ID"));

   var ts = doc.getSheetByName("dir");   var source =
 doc.getSheetByName("data");   var names =
 source.getDataRange().getValues();   var data =
 ts.getDataRange().getValues();   var arr = [];   var arr2 = [];   var
 arr3 = [];   var arr4 = [];
      for (i=0; i<names.length; i++) {

     for (j=0; j<data.length; j++) {

           stringSimilarity.compareTwoStrings(names[i][0], data[j][0]);   Logger.log(stringSimilarity);




       /*
       var n = data[j][0].split();
       for (N=0; N<n.length; N++) {
       arr2.push(n[N].charAt(0));
       }
       var string2 = arr2.join("");
       var arr2 = [];

       if (names[i][0] === data[j][0]) {

       arr.push([data[j][1]]);

       }      */ //I want to replace this blanked out code with String >similarity.      

       if (string === string2) {

         arr.push([data[j][1]]);
         arr3.push([i+1]);
         arr4.push([data[j][0]]);


       }
     }   }   for (i = 0; i<arr.length; i++) {
     source.getRange(arr3[i],6).setValue(arr[i]);
     source.getRange(arr3[i],7).setValue(arr4[i]);   }    }
like image 724
Alejandro Jurado Avatar asked Mar 10 '18 01:03

Alejandro Jurado


People also ask

How do I use libraries in Google Apps Script?

Select Resources > Libraries... Paste in the project key of the library that you want to use into the Add a Library text box. Click Add to add the library to your project. If you encounter an authorization error, make sure that you have at least read-level access to the project that you are trying to include.

Can I use Python in Google App script?

Python 2.6 or greater. The pip package management tool. A Google Cloud Platform project with the API enabled. To create a project and enable an API, refer to Create a project and enable the API.


2 Answers

Your GAS project is not a Node.js app, so the above will not work. While both Google Apps Script and Node use JavaScript, they provide different runtime environments for executing JS code. In GAS, the environment is a closed ecosystem on Google Servers that end users don't know anything about.

In Node, the runtime consists of V8 (JS engine) and C++ add-ons that expose low-level APIs (access to the file system, etc.). The library you referenced is an NPM package created for Node.js. Installing the package via NPM will make it available for Node projects, but don't think it will magically appear on Google servers as well.

You must either use GAS-specific versions of these dependencies, or, if they don't exist, refactor the source code to make it compatible with GAS (Node and GAS use different ECMAScript versions, so some latest features like arrow functions will break your GAS code).

For example, here's lodash for GAS https://github.com/contributorpw/lodashgs

Using libraries in GAS https://developers.google.com/apps-script/guides/libraries

P.S. In GAS, all ".gs" files share the same namespace, so calling 'require' function is redundant. If you want to mimic this behavior, you'll still need to write your own require function.

like image 70
Anton Dementiev Avatar answered Oct 13 '22 12:10

Anton Dementiev


I wrote up some guidance on packaging up an npm module for usage in Apps Script in this article.

tl;dr is to create an index.js with the following in a new directory:

import {compareTwoStrings, findBestMatch} from 'string-similarity';
export {compareTwoStrings, findBestMatch};

and then run the following in that directory:

npm init -y
npm install --save-dev string-similarity
npx esbuild index.js --bundle --global-name=stringSimilarity --outfile=StringSimilarity.js

You can then copy the contents of StringSimiliarity.js into a new .gs file in the Apps Script editor. The exported functions will be usable from your own Code.gs as stringSimilarity. compareTwoStrings() and stringSimilarity.findBestMatch().

(In case you'd rather not bundle it yourself, the output of the esbuild process for string-similarity can be found in this gist. But the general steps should apply to most npm modules that don't require a Node or browser-specific runtime environment.)

like image 20
Jeff Posnick Avatar answered Oct 13 '22 13:10

Jeff Posnick