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 userequire
,import
or instantiate this guy.– BrTkCa
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.
– Gabriel Rodrigues