smileysquirrel837
smileysquirrel837

Reputation: 13

How to Ensure Imported Numeric Data in Google Sheets Using Apps Script?

I am pretty new to apps script with google sheets and am wondering if there is a way to change my object type of values to numeric before importing into my google sheets. I have a function that takes a csv, parses and gives me what I want. But when calling the function in google sheets the imported numeric data is not numeric and mathematical sheets functions won't work. I know I can do an array formula in sheets and work that way, but is there a way to directly import the data into sheets as numbers? Sheet pictured below, code as well.

Sheet Ex

I have worked around using =arrayformula(cell+0) to essentially copy all of the data in the imported table and work with it then, but am thinking there has to be a more efficient way such as importing it directly as numeric.

function ImportPlayerMPLog(playerID) {
 // Get the Link to the CSV file for player with Player ID Number
 let link = 'https://www.moneypuck.com/moneypuck/playerData/careers/gameByGame/regular/skaters/'
 link += playerID
 link += '.csv'
  // Download the CSV


 var res = UrlFetchApp.fetch(link)
 var csvraw = res.getContentText()
 var csv = Utilities.parseCsv(csvraw)
 // Want only columns 0,1,2,3,4,5,6,7,8,9,32,33,34,35,36,37,49,52,53,54,86
 var DesiredCols = [0,1,2,3,4,5,6,7,8,9,10,11,30,31,32,33,34,35,36,37,49,52,53,54,86];


 var Columns = function ExtractCols(arr,indexArr) {
   return arr.map(obj => {
     return obj.filter((ob,index) => {
       if (indexArr.includes(index)) {return ob}
     })
   })
 }


 csv = Columns(csv,DesiredCols)




 var FilterLogic = function(item){
   if(item[1]>2021 && item[9] == 'all'){
     return true;
   } else {
     return false;
   }
 }
 var NewCSV = csv.filter(FilterLogic)
 return NewCSV
}

Upvotes: 1

Views: 53

Answers (1)

Tanaike
Tanaike

Reputation: 201388

Although I'm not sure whether I could correctly understand your question, how about the following modification?

From:

return NewCSV

To:

return NewCSV.map(r => r.map(c => !isNaN(c) ? Number(c) : c));

References:

Upvotes: 0

Related Questions