简体   繁体   中英

Google Apps Script - Converting column of data into an array

I have a program that works as below

function test() {
var urls = [
    'URL1, 
    'URL2',
    'URL3'
  ]

var results = xx(urls)

^and then xx works its magic and produces results.

Now I want to input the urls via a column in a spreadsheet instead of hard coding them into the script, and so I tried

 function test() {
    var urls = []
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Input")
    urls[0] = ss.getRange(2, 1, ss.getLastRow()-1,1).getValues().join()
    
    var results = xx(urls)

But the result is that only one of the URLs gets processed.

Explanation:

You haven't provided what this magical xx function does, but according to the first code snippet, xx accepts a single array of elements.

Therefore, one way of going from 2D array to 1D is to use flat

Solution:

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Input")
  const urls = ss.getRange(2, 1, ss.getLastRow()-1).getValues().flat(); // 1D array
  const results = xx(urls);
}

Not sure this is the most elegant solution, but got it to work by modifying it:

function test() {
var urls = {}
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Input")
urls = ss.getRange(2, 1, ss.getLastRow()-1,1).getValues();
urls = [].concat.apply([], urls)

var results = xx(urls)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM