Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

javascript - 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.

question from:https://stackoverflow.com/questions/65944069/google-apps-script-converting-column-of-data-into-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...