I am complete noob to scripts, java and all things code but i need to get a script working on a google spreadsheet of mine.
Its a file inventory and I need to convert raw bytes to KB, MB,GB.
I tried a formatting ajustment on the cells themselves but that gave me incorrect calculations something to do with 1000 vs 1024 values
I have a script that works im just not sure how to get it into my spreadsheet.
(Spare me corretions on my improper use of coding grammar, I am not trying to become a better coder I just want to get this thing working...)
Here's what I want to implement to my google sheet
function formatBytes(bytes, decimals = 2) {
    if (bytes === 0) return '0 Bytes';
    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
} 
Can anyone help?
Some googling lead me to: https://gist.github.com/loganvolkers/933af8513ed8c2268f59c85a31761a43
Select your cells, go to Format menu, then Number, then Custom Number format and paste the following:
[<1000000]0.00," KB";[<1000000000]0.00,," MB";0.00,,," GB"
Credits to @loganvolkers.
This is how it looks like (applied to the 2nd column)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With