How to use the UrlFetchApp class with node.js

0

I'm trying to use a Google Apps Script class, but I'm having trouble finding information on how to use the classes contained in Apps Scripts and running on the node.

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

Result:

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

I have a problem with this:

asked by anonymous 03.04.2017 / 21:03

1 answer

0

The method you're looking for ( UrlFetchApp ) is part of of 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 . This means that all code written in this suite should necessarily run on the Google platform in the cloud. They even have a different file format for their scripts ( .gs , see an example here ).

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

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

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

In summary, it seems to me that you are trying to do something that is not possible. I suggest reviewing your source or better exemplifying your problem.

    
05.04.2017 / 15:16