Home > OS >  Convert comma delimited to array for setValues
Convert comma delimited to array for setValues

Time:08-02

I want to convert this comma delimited string:

test1,test2,test3\ntest11,test22,test33

to this so i can use it to setValues() on appscript

[["test1","test2","test3"],["test11","test22","test33"]]

I have this code below which works but it's putting quotes on the sheet. Im sure there's an easier way to do this.

function test() {
  var urls = 'test1,test2,test3\ntest11,test22,test33'
  var urlsArr = urls.split(/\n/g)
  var ssResultTotal = ssResult.getLastRow()
  var urlsArrFinal = []
  urlsArr.forEach(function (d, i) {
    var urlsArrSplit = d.split(',')
    var urlsArr2 = []
    urlsArrSplit.forEach(function (dd, ii) {
      urlsArr2.push('"' dd '"')
    })
    
    urlsArrFinal.push(urlsArr2)
    // urlsArrFinal.push(d)
    // Logger.log(urlsArr2)
  })
   ssResult.getRange(2,1,urlsArr.length,3).setValues(urlsArrFinal)
  

  Logger.log(JSON.stringify(urlsArrFinal))

}

CodePudding user response:

After you split the newline delimited string, split the comma delimited strings again with enter image description here

  • Related