在Google App脚本中使用导入的模块 [英] Using an imported module inside Google App Script

查看:83
本文介绍了在Google App脚本中使用导入的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google App脚本中使用字符串相似性,但是并非如此我完全清楚如何在App Script中使用它,我遇到了多个错误,例如未定义 require,另外请注意,模块本身似乎具有依赖性。

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.

我的最终目标是使用此脚本在App Script中将字符串中充满错字的数组与具有正确字符串的数组之间的字符串分数进行匹配。这是我失败的代码。

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]);   }    }


推荐答案

您的GAS项目不是Node.js应用程序,因此上述操作将无效。虽然Google Apps脚本和Node都使用JavaScript,但是它们提供了不同的运行时环境来执行JS代码。在GAS中,环境是Google服务器上的封闭生态系统,最终用户一无所知。

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.

在Node中,运行时由V8(JS引擎)和公开低级API(访问文件系统等)的C ++附加组件。您引用的库是为Node.js创建的NPM软件包。通过NPM安装该软件包将使其可用于Node项目,但不要以为它也会神奇地出现在Google服务器上。

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.

您必须使用特定于GAS的这些依赖项的版本,或者如果不存在,请重构源代码以使其与GAS兼容(Node和GAS使用不同的ECMAScript版本,因此某些最新功能(例如箭头功能)会破坏您的GAS代码)。

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).

例如,这是GAS
的lodash https:/ /github.com/contributorpw/lodashgs

在GAS中使用库
https://developers.google.com/apps-script/guides/libraries

PS在GAS中,所有 .gs文件共享相同的名称空间,因此调用 require功能是多余的。如果您想模仿这种行为,则仍然需要编写自己的require函数。

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.

这篇关于在Google App脚本中使用导入的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆