1

I'm fairly new to google scripts and I'm having trouble updating a row then moving on to the next row in the column in google sheets... The code below updates the cell(A3), however when you run the code again it updates the same row. My hunch is the A3 part... not too familiar with the google sheets syntax though... any help would be appreciated!

function getProfolioPrices() {
  SpreadsheetApp.getActiveSheet().getRange('A3').setValue('=GOOGLEFINANCE("TTWO", "price")');
  SpreadsheetApp.getActiveSheet().getRange('C3').setValue('=GOOGLEFINANCE("TSLA", "price")');
  SpreadsheetApp.getActiveSheet().getRange('E3').setValue('=GOOGLEFINANCE("AMZN", "price")');
  SpreadsheetApp.getActiveSheet().getRange('G3').setValue('=GOOGLEFINANCE("NEP", "price")'); 
}
1
  • Sorry, was in my own world at that time.
    – Skurpiel
    Sep 21, 2018 at 0:29

1 Answer 1

3

You can make use of script properties documented here. Script properties can be manually configured in File > Project properties > Script properties. You can use it to keep track of the next row you want to update, for example

function getProfolioPrices() {
  var activeSheet   = SpreadsheetApp.getActiveSheet();
  var nextRowNumber = getNextRowNumber();

  activeSheet.getRange('A' + nextRowNumber).setValue('=GOOGLEFINANCE("TTWO", "price")');
  activeSheet.getRange('C' + nextRowNumber).setValue('=GOOGLEFINANCE("TSLA", "price")');
  activeSheet.getRange('E' + nextRowNumber).setValue('=GOOGLEFINANCE("AMZN", "price")');
  activeSheet.getRange('G' + nextRowNumber).setValue('=GOOGLEFINANCE("NEP", "price")'); 
}

function getNextRowNumber(){
  var scriptProperties = PropertiesService.getScriptProperties();
  var nextRowCounter = scriptProperties.getProperty( 'nextRowCounter' );

  if ( isNaN( parseInt( nextRowCounter ) ) ) {
    nextRowCounter = 1; //Default to 1
  } else {
    nextRowCounter++;
  }

  scriptProperties.setProperty( 'nextRowCounter', nextRowCounter );

  return nextRowCounter;
}
1
  • Thank you, so much this exactly what I needed!
    – Skurpiel
    Sep 21, 2018 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.