Reputation: 491
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.
Upvotes: 0
Views: 365
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