Forum Discussion
Getting a value by dividing sum of columns in SharePoint list
- AnonymousJan 21, 2019
Hey Kjay! I took a glance at your screenshot and assumed a few things. First, you are using the Totals tool from the list view to display the numbers on the top, right? Second, that is a classic list view, aka classic layout. Third, you are using jQuery(I can see some nice formatting so I assume you are using it to fix stuff). If either one of these are not true, this may not work for you, tell me so we can try to work this out.
Now, the solution. If you open the Developer Tools and inspect the ribbon that shows the totals, you may see that it's actually a <tbody> tag appended to the table that shows the results, before the tbody that actually shows the results. This ribbon is a <tr> tag with a static ID, so it was pretty easy to query it using jQuery.
The function I included in the attachment basically picks the text of each one of the Totals row, calculate the average and set the value to the Average column.
All you need to do is call it and provide the index of the columns containing the Total, the Sum, and the Average columns. It's important to note that the checkmark that shows when you hover the title of a row and the 3 dots("...") for the options do count as columns, so you have to include those to figure the index of your columns.
Seeing your screenshot I would guess your columns A, B, and C are at indexes 4, 9, and 13. Call the function I provided and see if it works for you.
One thing to note is that this function only changes the values when it is called, so you may want to call it every time the view changes.
Well, I am having trouble uploading the code so I will just paste it here. Lemme know if you need anything. I would suggest opening your developer tools and testing the code before inserting it to the page to see if it works.
function calcAverage(sum_column_index, total_column_index, average_column_index){
var rows = $('#aggWPQ1 td');
var hash = rows.map((i, target) => {return {name: $(target).text().split("=")[0], value: $(target).text().split("=")[1]}});
var sum = parseFloat(hash[sum_column_index].value.replace(/[^0-9.-]+/g,""));
var total = parseFloat(hash[total_column_index].value.replace(/[^0-9.-]+/g,""));
var avg = (sum / total).toFixed(2);
rows.eq(average_column_index).html("<strong>" + hash[average_column_index].name + '= ' + avg + "</strong>")}
Hope it helps,
Thanks Carlis.... I don't mind coding at all. I will do whatever it needs. Just let me know how can I do it. Thanks
Hey Kjay! I took a glance at your screenshot and assumed a few things. First, you are using the Totals tool from the list view to display the numbers on the top, right? Second, that is a classic list view, aka classic layout. Third, you are using jQuery(I can see some nice formatting so I assume you are using it to fix stuff). If either one of these are not true, this may not work for you, tell me so we can try to work this out.
Now, the solution. If you open the Developer Tools and inspect the ribbon that shows the totals, you may see that it's actually a <tbody> tag appended to the table that shows the results, before the tbody that actually shows the results. This ribbon is a <tr> tag with a static ID, so it was pretty easy to query it using jQuery.
The function I included in the attachment basically picks the text of each one of the Totals row, calculate the average and set the value to the Average column.
All you need to do is call it and provide the index of the columns containing the Total, the Sum, and the Average columns. It's important to note that the checkmark that shows when you hover the title of a row and the 3 dots("...") for the options do count as columns, so you have to include those to figure the index of your columns.
Seeing your screenshot I would guess your columns A, B, and C are at indexes 4, 9, and 13. Call the function I provided and see if it works for you.
One thing to note is that this function only changes the values when it is called, so you may want to call it every time the view changes.
Well, I am having trouble uploading the code so I will just paste it here. Lemme know if you need anything. I would suggest opening your developer tools and testing the code before inserting it to the page to see if it works.
function calcAverage(sum_column_index, total_column_index, average_column_index){
var rows = $('#aggWPQ1 td');
var hash = rows.map((i, target) => {return {name: $(target).text().split("=")[0], value: $(target).text().split("=")[1]}});
var sum = parseFloat(hash[sum_column_index].value.replace(/[^0-9.-]+/g,""));
var total = parseFloat(hash[total_column_index].value.replace(/[^0-9.-]+/g,""));
var avg = (sum / total).toFixed(2);
rows.eq(average_column_index).html("<strong>" + hash[average_column_index].name + '= ' + avg + "</strong>")
}
Hope it helps,
- KJay1Jan 23, 2019Copper Contributor
Works like a charm!!!!!... thank you so much for your help Carlis... I really appreciate it so much. You are a lifesaver....
- AnonymousJan 24, 2019You're welcome! Happy to see it could help you!
Regards,
- KJay1Jan 22, 2019Copper Contributor
Thank you so much for your great help Carlis. I will try it out and get back to you in a day or two. Thanks again... really appreciate it.