Fletch Posted September 13, 2010 Share Posted September 13, 2010 Does anyone have code created to convert the Postal Abbreviation to the spelled out State name? (i.e. AR is in the data base, I need Arkansas to be made in the variable. Quote Link to comment Share on other sites More sharing options...
rpaterick Posted September 13, 2010 Share Posted September 13, 2010 Dan or someone else could easily clean this up and to account for casing issues that your data may have. If the data is lowercase al for example, it will just return al and not Alabama. Otherwise it works. Oh yeah, Dan, why is it between District of Columbia and Florida does it produce another record during testing of all states? if (Field ("state") == "AL") return "Alabama"; if (Field("state") == "AK") return "Alaska"; if (Field("state") == "AZ") return "Arizona"; if (Field("state") == "AR") return "Arkansas"; if (Field("state") == "CA") return "California"; if (Field("state") == "CO") return "Colorado"; if (Field("state") == "CT") return "Connecticut"; if (Field("state") == "DE") return "District of Columbia"; if (Field("state") == "FL") return "Florida"; if (Field("state") == "GA") return "Georgia"; if (Field("state") == "HI") return "Hawaii"; if (Field("state") == "ID") return "Idaho"; if (Field("state") == "IL") return "Illinois"; if (Field("state") == "IN") return "Indiana"; if (Field("state") == "IA") return "Iowa"; if (Field("state") == "KS") return "Kansas"; if (Field("state") == "KY") return "Kentucky"; if (Field("state") == "LA") return "Louisiana"; if (Field("state") == "ME") return "Maine"; if (Field("state") == "MD") return "Maryland"; if (Field("state") == "MA") return "Massachusetts"; if (Field("state") == "MI") return "Michigan"; if (Field("state") == "MN") return "Minnesota"; if (Field("state") == "MS") return "Mississippi"; if (Field("state") == "MO") return "Missouri"; if (Field("state") == "MT") return "Montana"; if (Field("state") == "NE") return "Nebraska"; if (Field("state") == "NV") return "Nevada"; if (Field("state") == "NH") return "New Hamspire"; if (Field("state") == "NJ") return "New Jersey"; if (Field("state") == "NM") return "New Mexico"; if (Field("state") == "NY") return "New York"; if (Field("state") == "NC") return "North Carolina"; if (Field("state") == "ND") return "North Dakota"; if (Field("state") == "OH") return "Ohio"; if (Field("state") == "OK") return "Oklahoma"; if (Field("state") == "OR") return "Oregon"; if (Field("state") == "PA") return "Pennsylvania"; if (Field("state") == "RI") return "Rhode Island"; if (Field("state") == "SC") return "South Carolina"; if (Field("state") == "SD") return "South Dakota"; if (Field("state") == "TN") return "Tennessee"; if (Field("state") == "TX") return "Texas"; if (Field("state") == "UT") return "Utah"; if (Field("state") == "VT") return "Vermont"; if (Field("state") == "VA") return "Virginia"; if (Field("state") == "WA") return "Washington"; if (Field("state") == "WV") return "West Virginia"; if (Field("state") == "WI") return "Wisconsin"; if (Field("state") == "WY") return "Wyoming"; else return Field("state"); Quote Link to comment Share on other sites More sharing options...
esmith Posted September 14, 2010 Share Posted September 14, 2010 // ... if (Field("state") == "CT") return "Connecticut"; if (Field("state") == "DE") return "District of Columbia"; if (Field("state") == "FL") return "Florida"; // ... You have "DE" set to return "District of Columbia" when It should be "Delaware". Also, to deal with various casing in data you could add: var state = Field("State").toUpperCase(); and then change all your IF conditions to test against the variable instead of the raw field data. Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted September 14, 2010 Share Posted September 14, 2010 Dan or someone else could easily clean this up and to account for casing issues that your data may have. If the data is lowercase al for example, it will just return al and not Alabama. Otherwise it works. if (Field ("state") == "AL") return "Alabama"; if (Field("state") == "AK") return "Alaska"; // etc... if (Field("state") == "WI") return "Wisconsin"; if (Field("state") == "WY") return "Wyoming"; else return Field("state"); That will work, but if you ever need to change anything like the field name, then you'll have to make 50-plus modifications to the rule. This kind of thing is exactly what the switch statement was made for, so that you can reduce all those chained "if" statements to something like this: switch (Field("state").toUpperCase()) { case "AL": return "Alabama"; case "AK": return "Alaska"; // etc... default: return Field("state"); } Although I'd use a solution that's basically the converse of this: http://forums.printable.com/showthread.php?p=5587#post5587 var StateNamesFromAbbreviations = { "AL": "Alabama", "AK": "Alaska", "AZ": "Arizona", "AR": "Arkansas", "CA": "California", "CO": "Colorado", "CT": "Connecticut", "DE": "Delaware", "DC": "District of Columbia", "FL": "Florida", "GA": "Georgia", "HI": "Hawaii", "ID": "Idaho", "IL": "Illinois", "IN": "Indiana", "IA": "Iowa", "KS": "Kansas", "KY": "Kentucky", "LA": "Louisiana", "ME": "Maine", "MD": "Maryland", "MA": "Massachusetts", "MI": "Michigan", "MN": "Minnesota", "MS": "Mississippi", "MO": "Missouri", "MT": "Montana", "NE": "Nebraska", "NV": "Nevada", "NH": "New Hamspire", "NJ": "New Jersey", "NM": "New Mexico", "NY": "New York", "NC": "North Carolina", "ND": "North Dakota", "OH": "Ohio", "OK": "Oklahoma", "OR": "Oregon", "PA": "Pennsylvania", "RI": "Rhode Island", "SC": "South Carolina", "SD": "South Dakota", "TN": "Tennessee", "TX": "Texas", "UT": "Utah", "VT": "Vermont", "VA": "Virginia", "WA": "Washington", "WV": "West Virginia", "WI": "Wisconsin", "WY": "Wyoming", }; return StateNamesFromAbbreviations[ToUpper(Field("State"))] || Field("State"); Oh yeah, Dan, why is it between District of Columbia and Florida does it produce another record during testing of all states? Sorry, I don't know what you mean. Specifically what testing are you doing? Quote Link to comment Share on other sites More sharing options...
traba5058 Posted November 8, 2012 Share Posted November 8, 2012 This is great information because I am trying to do this now. I am setting up a business card that has the address along the bottom of the card in 1 line. They actually can have 2 addresses, so I have dupplicated the rules below for Office Location 1 and Office Location 2. The client also wants the state to be spelled out. I am trying to combine this rule with my two state rules. Rule 1: Proper Case State var Var1 = "State"; var CaseSelection = "propercase"; if(CaseSelection == "allcaps") return ToUpper(Field(Var1)); if(CaseSelection == "smallcaps") return "<smallcap>" + Field(Var1) + "</smallcap>"; if(CaseSelection == "propercase") return ToTitleCase(Field(Var1)); if(CaseSelection == "lowercase") return ToLower(Field(Var1)); Rule 2: Adds a Comma and a Space before State if (Rule("StateProperCaseRule") == "") return ""; else return ', '+Rule("StateProperCaseRule"); Can you help? Thanks, Traba Quote Link to comment Share on other sites More sharing options...
esmith Posted November 8, 2012 Share Posted November 8, 2012 I am trying to combine this rule with my two state rules. Rule 1: Proper Case State var Var1 = "State"; var CaseSelection = "propercase"; if(CaseSelection == "allcaps") return ToUpper(Field(Var1)); if(CaseSelection == "smallcaps") return "<smallcap>" + Field(Var1) + "</smallcap>"; if(CaseSelection == "propercase") return ToTitleCase(Field(Var1)); if(CaseSelection == "lowercase") return ToLower(Field(Var1)); Rule 2: Adds a Comma and a Space before State if (Rule("StateProperCaseRule") == "") return ""; else return ', '+Rule("StateProperCaseRule"); I'm confused. Based on the two rules you posted, it sounds as though your data already has the state spelled out in the data? Assuming the data passes state abbreviations you could just have one rule: var StateNamesFromAbbreviations = { "AL": "Alabama", "AK": "Alaska", "AZ": "Arizona", "AR": "Arkansas", "CA": "California", "CO": "Colorado", "CT": "Connecticut", "DE": "Delaware", "DC": "District of Columbia", "FL": "Florida", "GA": "Georgia", "HI": "Hawaii", "ID": "Idaho", "IL": "Illinois", "IN": "Indiana", "IA": "Iowa", "KS": "Kansas", "KY": "Kentucky", "LA": "Louisiana", "ME": "Maine", "MD": "Maryland", "MA": "Massachusetts", "MI": "Michigan", "MN": "Minnesota", "MS": "Mississippi", "MO": "Missouri", "MT": "Montana", "NE": "Nebraska", "NV": "Nevada", "NH": "New Hamspire", "NJ": "New Jersey", "NM": "New Mexico", "NY": "New York", "NC": "North Carolina", "ND": "North Dakota", "OH": "Ohio", "OK": "Oklahoma", "OR": "Oregon", "PA": "Pennsylvania", "RI": "Rhode Island", "SC": "South Carolina", "SD": "South Dakota", "TN": "Tennessee", "TX": "Texas", "UT": "Utah", "VT": "Vermont", "VA": "Virginia", "WA": "Washington", "WV": "West Virginia", "WI": "Wisconsin", "WY": "Wyoming", }; // return the proper case state name if data matches array value // or the value in the data if no match is found var result = StateNamesFromAbbreviations[ToUpper(Field("State"))] || Field("State"); //return result preceded by a comma and space return ", " + result; Of course, you could probably build the entire address(es) line(s) in one rule if we knew what you were trying to accomplish in its entirety. Quote Link to comment Share on other sites More sharing options...
traba5058 Posted November 8, 2012 Share Posted November 8, 2012 Thanks Eric, I just replaced my State Rule with [font=Courier New][color=#000000]var StateNamesFromAbbreviations =[/color][/font] [font=Courier New][color=#000000]{[/color][/font] [font=Courier New][color=#000000] "AL": "Alabama",[/color][/font] [font=Courier New][color=#000000] "AK": "Alaska",[/color][/font] [font=Courier New][color=#000000] "AZ": "Arizona",[/color][/font] [font=Courier New][color=#000000] "AR": "Arkansas",[/color][/font] [font=Courier New][color=#000000] "CA": "California",[/color][/font] [font=Courier New][color=#000000] "CO": "Colorado",[/color][/font] [font=Courier New][color=#000000] "CT": "Connecticut",[/color][/font] [font=Courier New][color=#000000] "DE": "Delaware",[/color][/font] [font=Courier New][color=#000000] "DC": "District of Columbia",[/color][/font] [font=Courier New][color=#000000] "FL": "Florida",[/color][/font] [font=Courier New][color=#000000] "GA": "Georgia",[/color][/font] [font=Courier New][color=#000000] "HI": "Hawaii",[/color][/font] [font=Courier New][color=#000000] "ID": "Idaho",[/color][/font] [font=Courier New][color=#000000] "IL": "Illinois",[/color][/font] [font=Courier New][color=#000000] "IN": "Indiana",[/color][/font] [font=Courier New][color=#000000] "IA": "Iowa",[/color][/font] [font=Courier New][color=#000000] "KS": "Kansas",[/color][/font] [font=Courier New][color=#000000] "KY": "Kentucky",[/color][/font] [font=Courier New][color=#000000] "LA": "Louisiana",[/color][/font] [font=Courier New][color=#000000] "ME": "Maine",[/color][/font] [font=Courier New][color=#000000] "MD": "Maryland",[/color][/font] [font=Courier New][color=#000000] "MA": "Massachusetts",[/color][/font] [font=Courier New][color=#000000] "MI": "Michigan",[/color][/font] [font=Courier New][color=#000000] "MN": "Minnesota",[/color][/font] [font=Courier New][color=#000000] "MS": "Mississippi",[/color][/font] [font=Courier New][color=#000000] "MO": "Missouri",[/color][/font] [font=Courier New][color=#000000] "MT": "Montana",[/color][/font] [font=Courier New][color=#000000] "NE": "Nebraska",[/color][/font] [font=Courier New][color=#000000] "NV": "Nevada",[/color][/font] [font=Courier New][color=#000000] "NH": "New Hamspire",[/color][/font] [font=Courier New][color=#000000] "NJ": "New Jersey",[/color][/font] [font=Courier New][color=#000000] "NM": "New Mexico",[/color][/font] [font=Courier New][color=#000000] "NY": "New York",[/color][/font] [font=Courier New][color=#000000] "NC": "North Carolina",[/color][/font] [font=Courier New][color=#000000] "ND": "North Dakota",[/color][/font] [font=Courier New][color=#000000] "OH": "Ohio",[/color][/font] [font=Courier New][color=#000000] "OK": "Oklahoma",[/color][/font] [font=Courier New][color=#000000] "OR": "Oregon",[/color][/font] [font=Courier New][color=#000000] "PA": "Pennsylvania",[/color][/font] [font=Courier New][color=#000000] "RI": "Rhode Island",[/color][/font] [font=Courier New][color=#000000] "SC": "South Carolina",[/color][/font] [font=Courier New][color=#000000] "SD": "South Dakota",[/color][/font] [font=Courier New][color=#000000] "TN": "Tennessee",[/color][/font] [font=Courier New][color=#000000] "TX": "Texas",[/color][/font] [font=Courier New][color=#000000] "UT": "Utah",[/color][/font] [font=Courier New][color=#000000] "VT": "Vermont",[/color][/font] [font=Courier New][color=#000000] "VA": "Virginia",[/color][/font] [font=Courier New][color=#000000] "WA": "Washington",[/color][/font] [font=Courier New][color=#000000] "WV": "West Virginia",[/color][/font] [font=Courier New][color=#000000] "WI": "Wisconsin",[/color][/font] [font=Courier New][color=#000000] "WY": "Wyoming",[/color][/font] [font=Courier New][color=#000000]};[/color][/font] [font=Courier New][color=#000000] [/color][/font] [font=Courier New][color=#000000]// return the proper case state name if data matches array value // or the value in the data if no match is found var result = StateNamesFromAbbreviations[ToUpper(Field("State"))] || Field("State");[/color][/font] [font=Courier New][color=#000000] [/color][/font] [font=Calibri][color=#000000]//return result preceded by a comma and space return ", " + result;[/color][/font] I get a validation error now "Function Does Not Return a Value." Quote Link to comment Share on other sites More sharing options...
esmith Posted November 8, 2012 Share Posted November 8, 2012 That's not the rule as I provided it. You have inadvertently removed line breaks between comments and code which is the cause of your problem. Quote Link to comment Share on other sites More sharing options...
traba5058 Posted November 8, 2012 Share Posted November 8, 2012 Thanks Eric! I recopied & pasted, and it works. Have a great day and thanks for your quick reply! Quote Link to comment Share on other sites More sharing options...
traba5058 Posted November 8, 2012 Share Posted November 8, 2012 Eric, Can you help me one more time? I duplicated the rule for the State field for my 2nd location address to var result = StateNamesFromAbbreviations[ToUpperField("O2State"))] || Field("O2State"); Now I am getting a validation error that states Line 58 Syntax Error missing ] in index expression, and the 2nd ) is highlighted. If I delete the second ), I get a validation error that states Line 58 Reference Error To Upper field is not defined. Thanks, Traba Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted November 8, 2012 Share Posted November 8, 2012 var result = StateNamesFromAbbreviations[ToUpperField("O2State"))] || Field("O2State"); Now I am getting a validation error that states Line 58 Syntax Error missing ] in index expression, and the 2nd ) is highlighted. If I delete the second ), I get a validation error that states Line 58 Reference Error To Upper field is not defined. You have to be more careful about copy-and-pasting. That line should read: var result = StateNamesFromAbbreviations[ToUpper(Field("O2State"))] || Field("O2State"); Quote Link to comment Share on other sites More sharing options...
traba5058 Posted November 8, 2012 Share Posted November 8, 2012 Thanks Dan! It is working now, but it returns a comma & space when the field is blank. Can you tell me how to get rid of that? Most of the time they will not have 2 office locations, so they will not have the second state field populated. The entire rule reads var StateNamesFromAbbreviations = { "AL": "Alabama", "AK": "Alaska", "AZ": "Arizona", "AR": "Arkansas", "CA": "California", "CO": "Colorado", "CT": "Connecticut", "DE": "Delaware", "DC": "District of Columbia", "FL": "Florida", "GA": "Georgia", "HI": "Hawaii", "ID": "Idaho", "IL": "Illinois", "IN": "Indiana", "IA": "Iowa", "KS": "Kansas", "KY": "Kentucky", "LA": "Louisiana", "ME": "Maine", "MD": "Maryland", "MA": "Massachusetts", "MI": "Michigan", "MN": "Minnesota", "MS": "Mississippi", "MO": "Missouri", "MT": "Montana", "NE": "Nebraska", "NV": "Nevada", "NH": "New Hamspire", "NJ": "New Jersey", "NM": "New Mexico", "NY": "New York", "NC": "North Carolina", "ND": "North Dakota", "OH": "Ohio", "OK": "Oklahoma", "OR": "Oregon", "PA": "Pennsylvania", "RI": "Rhode Island", "SC": "South Carolina", "SD": "South Dakota", "TN": "Tennessee", "TX": "Texas", "UT": "Utah", "VT": "Vermont", "VA": "Virginia", "WA": "Washington", "WV": "West Virginia", "WI": "Wisconsin", "WY": "Wyoming", }; // return the proper case state name if data matches array value // or the value in the data if no match is found var result = StateNamesFromAbbreviations[ToUpper(Field("O2State"))] || Field("O2State"); //return result preceded by a comma and space return ", " + result; Quote Link to comment Share on other sites More sharing options...
esmith Posted November 8, 2012 Share Posted November 8, 2012 You can replace the last line return ", " + result; with return (result != "") ? ", " + result : ""; which should only include the comma-space if your data contains a value. Quote Link to comment Share on other sites More sharing options...
traba5058 Posted November 8, 2012 Share Posted November 8, 2012 Thanks Eric! That worked! Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted November 8, 2012 Share Posted November 8, 2012 You can replace the last line return ", " + result;with return (result != "") ? ", " + result : "";which should only include the comma-space if your data contains a value. Or, at the very top of the rule, just do this: if (!Field("O2State")) return "";There's no point is trying to look up an empty property name in StateNamesFromAbbreviations. So if the field value is empty, just bail out early. Although, there are probably other field values, like the city name, which are empty on the second office location address line, so instead of special handling for empty values in the rule, you could just select that line in the Variable Text Editor, click "Paragraph", and check "Suppress if containing empty variables." Then you don't need to worry about empty values in the rule. Actually, if you do this, then you don't need the rule to return the comma and space either; you can just do that in the Text Editor too. Quote Link to comment Share on other sites More sharing options...
NickG Posted December 19, 2019 Share Posted December 19, 2019 I need the opposite. I am new to FusionPro and Marcom and we are trying to build several templates at the moment. One of them needs to change out the State name with the abbreviation. My thought is that we would switch the order in the array like so… var AbbreviationsFromStateNames = { "Alabama": "AL", "Alaska": "AK", "Arizona": "AZ", "Arkansas": "AK", "California": "CA", "Colorado": "CO", "Connecticut": "CT", "Delaware": "DE", Would this take care of my task? Thanks, Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted December 24, 2019 Share Posted December 24, 2019 I need the opposite. I am new to FusionPro and Marcom and we are trying to build several templates at the moment. One of them needs to change out the State name with the abbreviation. My thought is that we would switch the order in the array like so… var AbbreviationsFromStateNames = { "Alabama": "AL", "Alaska": "AK", "Arizona": "AZ", "Arkansas": "AK", "California": "CA", "Colorado": "CO", "Connecticut": "CT", "Delaware": "DE", Would this take care of my task? Thanks, Sure, although to be safe, I would make all the names all either upper- or lower-case, and call ToUpper or ToLower on the field value you're comparing to. For example: var AbbreviationsFromStateNames = { "alabama": "AL", "alaska": "AK", // etc. 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.