I am trying to use a Google script that retrieves 2 securities fields from GOOGLEFINANCE
and saves the output to a Google Sheet file. I need the script to also add the datetime to the first column of the Sheet.
I have created a basic Google Sheet with 3 columns:
- A is formatted to DateTime. It has column name date in row 1 and is empty in rows 2 onwards
- C has the column name price in row 1 and is empty in rows 2 onwards
- D has the column name pe in row 1 and is empty in rows 2 onwards
Here is my function:
function myStocks() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
sh.insertRowAfter(1);
sh.getRange("A2").setValue(new Date());
sh.getRange("C2").setFormula('=GOOGLEFINANCE("GOOG", "price")');
sh.getRange("D2").setFormula('=GOOGLEFINANCE("GOOG", "pe")');
}
Here is the output:
Date price pe
12/10/2017 22:44:31 1037.05 34.55
12/10/2017 22:43:24 1037.05 34.55
The output of columns C and D is correct. The output of column A is wrong. Every time I run the function, each new row is added ABOVE the last row:
- The first time I ran the function was at
12/10/2017 22:43:24
and it added that row first. - The second time I ran the function was
12/10/2017 22:44:31
BUT it added that row ABOVE the last row in the sheet - I wanted it to add the new row BELOW the last row.
Is there a way to auto fill the datetime downwards in a single column in GoogleSheets, using a script function?