Hey guys hope everyone's winning at life
Has anyone ever had to format the File Size column for a view?
I know how to process a bytes file length into friendly "1.5 MB" and etc. using a myriad of other languages like JavaScript, C# etc, enjoying the use of functions e.g.
function getFileSize(size) {
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};
But how could I achieve this in the JSON for a modern SharePoint List View format, please? I see that it offers operators and etc but how can I achieve it without a function?
The raw value of $File_x0020_Size
is a long number, and adding .displayText
outputs the same number, sadly.
Here's a preview of my Gallery view format and the JSON
{
"schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
"hideSelection": true,
"height": 175,
"width": 650,
"fillHorizontally": true,
"hideColumnHeader": true,
"formatter": {
"elmType": "div",
"style": {
"text-align": "left",
"display": "flex",
"font-family": "Arial",
"padding": "20px 20px 20px 20px",
"border": "solid 1px #e6e6e6",
"margin-top": "21px"
},
"children": [
{
"elmType":"img",
"style": {
"max-width": "200px",
"min-height": "100px"
},
"attributes":{
"src": "[$ProductImage.serverRelativeUrl]"
}
},
{
"elmType": "div",
"style": {
"flex": "1",
"margin-left": "20px",
"line-height": "1.62em"
},
"children": [
{
"elmType": "div",
"children": [
{
"elmType": "img",
"style": {
"width": "32px",
"height": "32px"
},
"attributes": {
"src": "='https://spoprod-a.akamaihd.net/files/fabric/assets/item-types/40/'+[$File_x0020_Type]+'.svg'"
}
},
{
"elmType": "a",
"style": {
"font-size": "16px",
"color": "#0078d4",
"text-decoration": "none",
"font-weight": "bold"
},
"attributes": {
"target": "_blank",
"href": "[$FileRef]"
},
"txtContent": "[$FileLeafRef]"
}
]
},
{
"elmType": "div",
"style": {
"margin-top": "15px",
"color": "#444444",
"font-size": "16px"
},
"children": [
{
"elmType": "span",
"txtContent": "='Estimated cost price: ' + toString([$EstimatedPrice])"
}
]
},
{
"elmType": "div",
"style": {
"margin-top": "15px",
"color": "#444444",
"font-size": "11px"
},
"children": [
{
"elmType": "span",
"txtContent": "=[$File_x0020_Size] + ' bytes'"
},
{
"elmType": "span",
"style": {
"margin-left": "20px"
},
"txtContent": "='Last modified ' + toLocaleDateString([$Modified])"
}
]
}
]
}
]
}
}
Thanks in advance