0

I have a document list with 2 columns. the first column is a category (Example: Menu, General, Information) The second is an image. I would like the image to be automatically calculated according to the selected category, but I can't do it. I am a novice and I am learning to use SharePoint 2019. What is the best solution ?

0

1.You could create a calculated column with following formula:

=IF(menu="a","<DIV><img src='http://xx/sites/xx/SiteAssets/image1.png'></DIV>","<DIV><img src='http://xx/sites/xx/SiteAssets/image2.jpg'></DIV>")

Remember to select Number:

enter image description here

2.Then add a Script Editor Web Part with following codes to write HTML in to calculated column and convert HTML tag to image:

<script type="text/javascript">
//
// Text to HTML
// Feedback and questions: Christophe@PathToSharePoint.com
//
var theTDs = document.getElementsByTagName("TD");
var i=0;
var TDContent = " ";
while (i < theTDs.length) {
try {
TDContent = theTDs&#91;i&#93;.innerText || theTDs&#91;i&#93;.textContent;
if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
theTDs[i].innerHTML = TDContent;
}
}
catch(err){}
i=i+1;
}
//
// ExpGroupRenderData overwrites the default SharePoint function
// This part is needed for collapsed groupings
//
function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
var tbody=document.getElementById("tbod"+groupName+"_");
var wrapDiv=document.createElement("DIV");
wrapDiv.innerHTML="<TABLE><TBODY id=\"tbod"+ groupName+"_\" isLoaded=\""+isLoaded+ "\">"+htmlToRender+"</TBODY></TABLE>";
var theTBODYTDs = wrapDiv.getElementsByTagName("TD"); var j=0; var TDContent = " ";
while (j < theTBODYTDs.length) {
try {
TDContent = theTBODYTDs&#91;j&#93;.innerText || theTBODYTDs&#91;j&#93;.textContent;
if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
theTBODYTDs[j].innerHTML = TDContent;
}
}
catch(err){}
j=j+1;
}
tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild,tbody);
}
</script>

My test result:

enter image description here

For Reference: Add image to custom list with calculated / conditional column

1
  • The first part seems to work, but when I try to add a script with the "script editor" web component I got this error in my console log : Uncaught SyntaxError: Invalid or unexpected token. I don't see where this error came from, since I have copy/paste your code.
    – TiagoPinto
    Sep 27 '21 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.