LisaHansen Posted April 21, 2022 Share Posted April 21, 2022 (edited) I'm working on a project where images are returned from an external data file. The user can make up to three choices from drop-downs. In the ExDF, several drop-down options may point to the same image file. The client does not want duplicate images, so I put together the code below. If options are chosen for all three fields, it works great. var externalDF = new ExternalDataFileEx("Competitor-ExDF.xlsx", "Excel"); var a = externalDF.FindRecord("CompetitorDropDown", Field("CP-Product-1-A")); var b = externalDF.FindRecord("CompetitorDropDown", Field("CP-Product-1-B")); var c = externalDF.FindRecord("CompetitorDropDown", Field("CP-Product-1-C")); var caseA = ((externalDF.GetFieldValue(a, "CompetitorImage"))); var caseB = ((externalDF.GetFieldValue(b, "CompetitorImage"))); var caseC = ((externalDF.GetFieldValue(c, "CompetitorImage"))); var resultA = Resource(externalDF.GetFieldValue(a, "CompetitorImage")).content; var resultB = Resource(externalDF.GetFieldValue(b, "CompetitorImage")).content; var resultC = Resource(externalDF.GetFieldValue(c, "CompetitorImage")).content; if ((caseA != caseB) && (caseA != caseC) && (caseB != caseC)) {return resultA + " " + resultB + " " + resultC;} if ((caseA == caseB) && (caseA != caseC)) {return resultA + " " + resultC;} if ((caseA == caseB) && (caseA == caseC)) {return resultA;} if ((caseA != caseB) && (caseA == caseC)) {return resultA + " " + resultB;} if ((caseA != caseB) && (caseA != caseC) && (caseB == caseC)) {return resultA + " " + resultB;} else return ""; I'm having an issue if they don't choose something for all three. Ideally, they should be able to choose just A or A and B if they'd like. Currently, if only one or two options are chosen, it breaks the whole table. This rule is just for the first row. The images appear side-by-side if there is more than one unique image. How can I change this so if there is nothing in choices B or C it will still return A (or A & B if only C is blank)? Thank you for looking! Edited April 21, 2022 by LisaHansen Added information Quote Link to comment Share on other sites More sharing options...
step Posted April 21, 2022 Share Posted April 21, 2022 Would this work? var externalDF = new ExternalDataFileEx("Competitor-ExDF.xlsx", "Excel"); var unique = function (value, index, self) { return value ? self.indexOf(value) === index : false; }; var getResource = function (value) { var fieldValue = externalDF.GetFieldValue(value, "CompetitorImage"); return fieldValue ? Resource(fieldValue).content : ''; }; return ["CP-Product-1-A", "CP-Product-1-B", "CP-Product-1-C"] .map(Field) .filter(unique) .map(getResource) .filter(String) .join(' '); Quote Link to comment Share on other sites More sharing options...
LisaHansen Posted April 21, 2022 Author Share Posted April 21, 2022 Thank you so much for checking this out, Step! Currently, my tag file has data in all three fields. With your code, I get this error: uncaught exception: Error: In Resource(), no resource named CompetitorImage. I put my code back and it works again (with all three fields having data). I did test yours with B & C having no data and received the same result as with all three populated. Quote Link to comment Share on other sites More sharing options...
step Posted April 22, 2022 Share Posted April 22, 2022 Whoops I forgot to get the record from the external data field. Try this: var externalDF = new ExternalDataFileEx("Competitor-ExDF.xlsx", "Excel"); var unique = function (value, index, self) { return value ? self.indexOf(value) === index : false; }; var getResource = function (value) { [color="Red"]var competitor = externalDF.FindRecord("CompetitorDropDown", value); if (competitor) { var competitorImage = externalDF.GetFieldValue(competitor, "CompetitorImage"); if (competitorImage) { return Resource(competitorImage).content; } } return '';[/color] }; return ["CP-Product-1-A", "CP-Product-1-B", "CP-Product-1-C"] .map(Field) .filter(unique) .map(getResource) .filter(String) .join(' '); Quote Link to comment Share on other sites More sharing options...
LisaHansen Posted April 22, 2022 Author Share Posted April 22, 2022 This one works if any or all of them exists, however, it's allowing the duplicate image. Unfortunately, that's the reason I fell down this rabbit hole. In my tag, I have three different choices for A, B & C, they all point to the same image in the ExDF. It's showing that image three times instead of just showing it once. Could you explain the logic in lines 2 & 3 for me, please? I've not seen that before and would love to understand what you had in mind there. I did try changing false to true in line 3, no dice. I appreciate you! Quote Link to comment Share on other sites More sharing options...
LisaHansen Posted April 22, 2022 Author Share Posted April 22, 2022 Hold up! Slight tweak and you did it!!! I flipped your filters at the bottom so .filter(String) was first and .filter(unique) was second. I figured it needed the resource before it could know if it was unique or not. You are brilliant! Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.