Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

javascript - 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]);   }    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...