Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
How to call an external API from Google Apps Script

I want to call the Steem API from inside a Google Apps Script function. I want to see the appropriate server response. But I get the following error instead.

GAS error

According to this documentation, I ran the following test.

// @see https://github.com/steemit/steem-js/tree/master/doc#browser

const getHelloWorld = () => {
  const SOURCE_CODE_URL = 'https://cdn.jsdelivr.net/npm/steem/dist/steem.min.js';
  const response = UrlFetchApp.fetch( SOURCE_CODE_URL, );
  const code = response.getContentText();
  const steem = eval( code, );

  steem.api.getAccounts([ 'ned', 'dan', ], ( error, response, ) => {
    Logger.log( error, response, );
  });
}

In my prior research, I read that this answer says:

You can do this: eval(UrlFetchApp.fetch("my url").getContentText())

How can I call the Steem API from inside Google Apps Script?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Summary from comments:

Here is a similar but different SO question about importing modules.

Summary from this article:

Using npm modules inside of Apps Script

2021-07-12

I recently was processing some data using Apps Script, and needed to parse out second-level domain info from a bunch of URLs. This is definitely not the job for regular expressions, but it's perfect for an npm module, psl, that uses the public suffix list.

But while Apps Script has come a long way, and features lots of ES2015+ goodness nowadays, it's not possible to pull in arbitrary code from npm and run it directly.

To work around this, I created the following index.js file locally, exporting the interface that I wanted to call from Apps Script:

import psl from 'psl';
import Url from 'url-parse';

export function parseHostname(sourceURL) {
  const url = new Url(sourceURL);
  return psl.parse(url.hostname);
}

Then, I installed the necessary dependencies from npm, and bundled this code up with esbuild:

npm init -y
npm install --save-dev psl url-parse punycode
npx esbuild index.js --bundle --global-name=psl --outfile=psl.js

I manually copied the contents of the psl.js file into a psl.gs file, alongside my main Code.gs file in the Apps Script editor. (This would be annoying if the bundled output changed frequently, but doing it once by hand wasn't a problem.)

Apps Script will automatically make the contents of all .gs files in a project visible in the same global scope, so I could now write code like

const {sld} = psl.parseHostname(url);

inside of my main Code.gs file.

Caveats

The Apps Script runtime environment still has a bunch of quirks when compared to Node, so don't expect all of your bundled code to work as-is. Polyfills may be needed (I used url-parse, for instance, since the native URL object isn't available in Apps Script.)

Once you copy over the bundled code to Apps Script, it's never going to be updated, so make sure you're prepared to rebundle if and when there are any security or feature updates to the modules you're using.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda