How to use the Urlfetchapp class with Node.js

Asked

Viewed 255 times

0

I’m trying to use a Google Apps Script class, but I’m having trouble finding information on how to use the classes in Apps Scripts and run in Ode.

Ex: converter.js

if (process.argv.length < 3) {
    console.log('Usage: node ' + process.argv[1] + ' FILENAME');
    process.exit(1);
}
// Read the file and print its contents.
var objects;
var fs = require('fs')
, filename = process.argv[2];

function print(item, index){
    var sourceLang = 'auto';
    var targetLang = 'pt';

    var sourceText = 'Traduzir';

    //var translatedText = LanguageApp.translate(item.mappings.default.default, sourceLang, targetLang);
    //console.log(translatedText);

    var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" 
    + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + encodeURI(sourceText);

    var result = JSON.parse(UrlFetchApp.fetch(url).getContentText());

    translatedText = result[0][0][0];
    console.log(translatedText);
    // console.log(item.mappings.default.default);
    // console.log(item.mappings.default.short);
}

fs.readFile(filename, 'utf8', function(err, data) {
    if (err) throw err;
  //console.log('OK: ' + filename);
  objects = JSON.parse(data);

  objects.forEach(print);

  console.log(objects.length);
  //console.log(data);
});

When I run with the command :

node conversor.js math_digits.json

Upshot :

/home/alcance/Área de Trabalho/conversor/conversor.js:23
    var result = JSON.parse(UrlFetchApp.fetch(url).getContentText());
                            ^

ReferenceError: UrlFetchApp is not defined
    at print (/home/alcance/Área de Trabalho/conversor/conversor.js:23:26)
    at Array.forEach (native)
    at /home/alcance/Área de Trabalho/conversor/conversor.js:36:11
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)

Well described the problem, I would like to know if anyone knows how I make this script work, or that you indicate me another class that can accomplish the same thing as Urlfetchapp in this context, I thank you in advance.

  • Where do you matter the UrlFetchApp? The error already tells you everything, you are trying to access something that does not exist. If you are imported into the project, you probably need to use require, import or instantiate this guy.

  • Yes, but I don’t know the require namespace, "require("?")", I haven’t found any example, and I also don’t know what I have to install by npm.

1 answer

0


The method you are looking for (Urlfetchapp) is part of the Google Apps Script. The description says:

Increase the power of your Favorite Google apps - like Calendar, Docs, Drive, Gmail, and Sheets. Apps Script Lets you do more with Google. All on a Javascript Platform in the cloud.

Attention to the point: All on a Javascript Platform in the cloud. That means all the code written on that suite must obligatorily run on the Google cloud platform. They even have a different file format for their scripts (.gs, see an example here).

This platform has a specific place to run the scripts, which is the Google Scripts. You can see in the description that they say, again, that these scripts run on their cloud platform, and all you need is browser.

That means, for me at least, that what you’re trying to do is impossible. There is nowhere in the GAS description references to connections to the Node or anything else. What exists are Standalone scripts that exist without link to any of the apps Google, but anyway they are "hosted" in your Gdrive.

There is also a package in NPM called Node Google Apps Script, but it only allows the use of your editor of choice to write your code.

In short, it seems to me that you are trying to do something that is not possible. I suggest you review your source or better illustrate your problem.

  • 1

    It just doesn’t seem possible, thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.