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

214
Visualizações
Updating rows in Google Sheets using Google script

I have the following Google Sheet with a tab called db:

enter image description here

In reality, the list is much longer. Eventually, I want to have a function triggered every hour to download data for 50 companies from Yahoo! Finance, as to not get rate restricted.

I am currently developing this function, and have the following in Code.gs:

function trigger() {
  var db =  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('db'); 
  var tickers = db.getRange('A2:A').getValues();

  for (var row = 1; row <= 5; row++) { 
    console.log(tickers[row])
    var data = yahoo(tickers[row]);
    db.getRange(row, 2, 1, 3).setValues(data);
  }
}

I have been messing around with different starting values for row, but just do not get the result as expected. Instead I get:

enter image description here

How can I:

  • not overwrite the cells in B1, C1, D1 but have the data be inserted behind the respective tickers?
  • run the loop not 5 times (as is now hardcoded), but only as many times as there are tickers? I tried row <= tickers.length() but got an error.
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try

function trigger() {
  var db = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('db');
  var tickers = db.getRange('A2:A' + db.getLastRow()).getValues().flat();

  for (var row = 0; row < tickers.length; row++) {
    console.log(tickers[row])
    var data = yahoo(tickers[row]);
    db.getRange(row + 2, 2, 1, 3).setValues(data);
  }
}

explanation: the index of an array (tickers) starts at 0, not 1, so, as we catch the tickers of line 2, you must add 2 to get the corresponding ticker.

about 4 years ago · Juan Pablo Isaza Relatório

0

When using the getRange(row, column, numRows, numColumns) method, keep in mind that the number 1 corresponds to row 1, so if you want to no ovewrite the headers, you must initialize it at 2.

It is also important to mention, that the method you are using is highly inefficient, since you are calling the service every time the loop iterates. If you are going to make this script call every hour, you may exceed the quotas. So I recommend that you use setValues(values).

Here is some pseudo-code that may help you.

const sS = SpreadsheetApp.getActiveSheet()

function fillWithoutLoops() {
  const tickers = sS.getRange('A2:A').getValues().flat().filter(n => n)
  const tickersParsed = tickers.map(yahoo)
  sS
    .getRange(2, 1, tickersParsed.length, tickersParsed[0].length)
    .setValues(tickersParsed)
}

function yahoo(ticker) {
  return [ticker, "SOME", "DATA", "MORE"]
}

Documentation

  • Use batch operation(Best practices in Google Apps Script)
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