1

Here's the problem:

Get a list of tickers from a column in a sheet (LIST OF STOCKS)

Push it, so as each ticker appears in the array 105 times

Write the resulting array to another sheet's column (TickersData)

Next to the first ticker each element brought to TickersData, add the formula.

Here's the code, where I could get up to repeating the values within an array, but can't write them to the other sheet:

function getGFTickersData() {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LIST OF STOCKS");
  var tickerRng = ss.getRange(2, 1, ss.getLastRow(), 1).getValues();
  var TDSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TickersData");
  var startRow = 2;
  var tickerArr = [];

  for (var b = 0; b < tickerRng.length; b++) {
    var tickerToArr = tickerRng[b];
    if (tickerRng[b] != '') {
      var repeated = [].concat(... new Array(104).fill([tickerToArr]));
      tickerArr.push(repeated);
    }
  }
  Logger.log(tickerArr.length);

  TDSheet.getRange(TDSheet.getLastRow() + 1, 1, tickerArr.length, 1).setValues(tickerArr);

  var targetRow = TDSheet.getRange("B:B").getValues();
  var maxIndex = targetRow.reduce(function (maxIndex, row, index) {
    return row[0] === "" ? maxIndex : index;
  }, 0);

  var row = TDSheet.getRange(maxIndex + 2, 2).setFormula("=query(googlefinance(" + '"' + tickerArr + '"' + ",'all shares'!A4,'all shares'!D3,'all shares'!D4,'all shares'!D5)," + '"' + "select *" + '"' + ",1)");
}

The logs

Here's an image of one ticker as an example. After 105 rows, there should be a new ticker in column A and the formula added next to it in column B: enter image description here

onit
  • 2,275
  • 11
  • 25
  • Unfortunately, I cannot understand about `Next to the first occurrence of element X, add the formula right next to it.` and your sample image, and `Right underneath VV, there should be the next array element and the formula set next to it`. I apologize for this. Can I ask you about the detail of your goal? – Tanaike Apr 03 '21 at 03:19
  • Hello @Tanaike! I just reqrote the question and it should be clearer now. Sorry about that. So the goal is to write the ticker 105 times in column A2 and the formula next to the first row of that ticker (B2). Than, the same with the ticker that will come underneath. So the next ticker should be written to row 107 and the formula set right next to it, in cell B107. Thank you! – onit Apr 03 '21 at 11:55
  • Thank you for replying. Unfortunately, I cannot understand about your goal. I apologize for this again. In order to correctly understand about your goal, can you provide the sample Spreadsheet including the sample input and output you expect? By this, I would like to try to understand it. – Tanaike Apr 04 '21 at 02:08
  • Hello @Tanaike! here's a practical example: https://docs.google.com/spreadsheets/d/1NZe359m4zH40ZtJ8O46FVl7yj5n90eiBBt1oM3uyk4Q/edit?usp=sharing Thank you very much – onit Apr 04 '21 at 19:20
  • Thank you for replying and adding more information. Unfortunately, when I saw your sample Spreadsheet, I cannot find the sample input and output situation. I apologize for this. Can I ask you about the detail of them? – Tanaike Apr 05 '21 at 01:08
  • Hello @Tanaike. The output is in sheet TickersData, highlighted in green background. This is what the script should do with each ticker being iterated over. Thank you so much! – onit Apr 05 '21 at 01:13
  • 1
    Thank you for replying. The sheet of "TickersData" is the output situation you expect? If my understanding is correct, can I ask you about the sample input situation? For example, to put the formula to the cell "B4" and "B109" is your goal? – Tanaike Apr 05 '21 at 01:17
  • This is correct! This is going to be put right next to the first occurrence of the ticker. – onit Apr 05 '21 at 01:25
  • 1
    Thank you for replying. In that case, the formula in your sample Spreadsheet is different from your script. For example, the cell "B4" is `=query(googlefinance("FCEL",B2,D2,E2,F2),"select *",1)`. But your script in your sample Spreadsheet is `var gFinFormula = "=query(googlefinance(" + '"' + tickerToArr + '"' + ",'all shares'!A4,'all shares'!D3,'all shares'!D4,'all shares'!D5)," + '"' + "select *" + '"' + ",1)";`. So I cannot understand about your goal. Can I ask you about the detail of your goal? – Tanaike Apr 05 '21 at 01:27
  • By the way, your this question is the same with your previous question of https://stackoverflow.com/q/66947088 ? – Tanaike Apr 05 '21 at 01:29
  • I've just corrected the formula to reflect my question. was trying to move forward and ended changing it and coming up with another question after I had made this question. Thank you! – onit Apr 05 '21 at 01:30
  • Thank you for replying. About `I've just corrected the formula to reflect my question.`, in this case, what formula do you want to use? About `was trying to move forward and ended changing it and coming up with another question after I had made this question.`, I understood that your this question is different from [your previous one](https://stackoverflow.com/q/66947088). – Tanaike Apr 05 '21 at 01:32
  • Hello, @Tanaike! The formula that is right is this one =query(googlefinance(" + '"' + tickerToArr + '"' + ",'all shares'!A4,'all shares'!D3,'all shares'!D4,'all shares'!D5)," + '"' + "select *" + '"' + ",1)" Thank you! – – onit Apr 05 '21 at 01:52
  • Thank you for replying. From your replying, I thought that I could understand about your goal. So I proposed a sample script as an answer. Could you please confirm it? If that was not the result you expect, I apologize. – Tanaike Apr 05 '21 at 02:02

1 Answers1

1

I believe your goal as follows.

  • You want to retrieve the values from "A2:A" from "LIST OF STOCKS" sheet.
  • You want to put the retrieved value to "Sheet10" sheet by including 105 rows that 1st row has the formula of =query(googlefinance("###",'all shares'!A4,'all shares'!D3,'all shares'!D4,'all shares'!D5),"select *",1).

In order to achieve your goal, I would like to propose the following sample script.

Sample script:

function getGFTickersData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LIST OF STOCKS");
  var tickerRng = ss.getRange(2, 1, ss.getLastRow() - 1, 1).getValues();
  var TDSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet10");
  var values = tickerRng.flatMap(([a]) => [[a, `=query(googlefinance("${a}",'all shares'!A4,'all shares'!D3,'all shares'!D4,'all shares'!D5),"select *",1)`], ...new Array(104).fill([a, ""])]);
  TDSheet.getRange(TDSheet.getLastRow() + 1, 1, values.length, 2).setValues(values);
}

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • You are fantastic! Thank you once again! This kind of help makes me want to learn more and more and not give up. I was wondering how you can access the file without me seeing who's logged in. :) – onit Apr 05 '21 at 02:08
  • @Antonio Santos Thank you for replying. I'm glad your issue was resolved. I think that your cooperation led the solution of your question. Thank you, too. – Tanaike Apr 05 '21 at 02:10