Tane
Tane

Reputation: 491

Parse and count total sum of numbers - Google Apps Script

I am exporting items to Google sheet. Item values include dot instead of comma, so I probably need to parse them first before I can count the total sum? What would be the best way to do this using Google Apps Script? I need to automate this process so I cannot use Google Sheets built in functions.

enter image description here

Upvotes: 0

Views: 365

Answers (1)

Cooper
Cooper

Reputation: 64032

Try this:

function myfunk1() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Your sheet name');
  const sum = sh.getRange(2,2,sh.getLastRow() -2,1).getDisplayValues().reduce((a,c)=>{ a+=parseFloat(c);return a;},0);
  sh.getRange(2,1,sh.getLastRow() - 1).getValues().forEach((r,i) => {if(r[0]=='sum')sh.getRange(i + 2,2).setValue(Number(sum).toFixed(2))});
} 

Ouput:

Item Cost
1 57.9
2 111.9
3 39.9
sum 209.7

Upvotes: 1

Related Questions