dreimer Posted September 18, 2019 Share Posted September 18, 2019 (edited) I have the following rule to create a UPC. The data usually comes in missing the leading zero. Now I have a mixture of barcodes where I don't need to add the leading zero for some records. zThe current code works great just need some code to add an if statement to add the zero if field is shorter than 11 characters and not to add it if the field has 11 characters. TIA. // Rule converted from XML Template "Barcode 1": // Choose the field(s) for your barcode. // Begin XML Template selections // var Var1 = "Barcode 1"; // "Choose a field:" (Required): FieldList var Var2 = ""; // "Choose another field (optional):": FieldList var Var3 = ""; // "Choose another field (optional):": FieldList var Var4 = ""; // "Choose another field (optional):": FieldList var Var5 = ""; // "Choose another field (optional):": FieldList var Var6 = "IDAutomationUPCEANS"; // "Pick a font:" (Required): FontList // End XML Template selections // retstr = "0"+Field(Var1); final_data = ""; if (Var2 != "") retstr += Field(Var2); if (Var3 != "") retstr += Field(Var3); if (Var4 != "") retstr += Field(Var4); if (Var5 != "") retstr += Field(Var5); final_data = '<span><f name="' + Var6 + '">' + NormalizeEntities(MakeUPCABarcode(retstr)) + '</span>'; return final_data; Also, is there a way to turn off the warning given during composition if a particular record doesn't have said barcode? I have 18 of these rules so some aren't always populated. Edited September 18, 2019 by dreimer Additional Question Quote Link to comment Share on other sites More sharing options...
ThomasLewis Posted September 18, 2019 Share Posted September 18, 2019 Change the line that is retstr = "0"+Field(Var1); to this: retstr = Field(Var1); if (retstr.length < 11) { retstr = "0" + retstr;} I'm not sure what the warning is but you might try this to replace that last part: if (Field(Var1)) { final_data = '<span><f name="' + Var6 + '">' + NormalizeEntities(MakeUPCABarcode(retstr)) + '</span>'; return final_data;} else { return "";} Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted September 18, 2019 Share Posted September 18, 2019 I have 18 of these rules so some aren't always populated. You mean you have fields like Barcode1, Barcode2, etc., all the way through Barcode18, and then 18 rules with those same names? To me, even with the XML templates, it seems like way too much work to make 18 different rules, and then to go modify all 18 when something changes. In other words, that kind of brute force way of doing things isn't easily maintainable. Instead, I would do something like this in OnRecordStart: for (var i = 1; i <= 18; i++) { var fieldName = "Barcode " + i; var data = Field(fieldName); var barcode = ""; if (data) { data = Right("00000000000" + data, 11); // pad to 11 chars with leading zeros barcode = '<span font="IDAutomationUPCEANS">' + TaggedTextFromRaw(MakeUPCABarcode(data)) + '</span>'; } FusionPro.Composition.AddVariable(fieldName, barcode, true); } Now, if something changes, like the names of the fields, or the number of them, or how you want the barcode to be output, all you need to do is change the logic in one place. All that said, the XML template rule could probably be a bit smarter and do that padding for you; I'll look into that enhancement. Quote Link to comment Share on other sites More sharing options...
dreimer Posted September 18, 2019 Author Share Posted September 18, 2019 Thank you very much for that, exactly what I needed, didn't think it was too difficult but I'm not a code guy!! The warning I get is a can't copyfit text in text boxes that have nothing in the fields, not sure I can avoid that. Quote Link to comment Share on other sites More sharing options...
dreimer Posted September 18, 2019 Author Share Posted September 18, 2019 Thanks Dan, that sounds like a much better way of doing things, I will try your code. Quote Link to comment Share on other sites More sharing options...
dreimer Posted September 18, 2019 Author Share Posted September 18, 2019 Dan, I tried your code, it didn't return any barcodes. I have my field names "Barcode 1", Barcode 2", etc. in the data. My text frames are named the same as the data fields. Where in the rule does it assign which text frame to place the barcode? Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted September 18, 2019 Share Posted September 18, 2019 Dan, I tried your code, it didn't return any barcodes. I have my field names "Barcode 1", Barcode 2", etc. in the data. My text frames are named the same as the data fields. Where in the rule does it assign which text frame to place the barcode? The rule doesn't return anything; it adds/inserts/injects variables with names "Barcode 1", "Barcode 2", etc. into the variables list. (This is sometimes called "variable injection.") These added variables can be used just like any others in text frames. They may not actually appear in the Variable drop-down on the Text Editor if you add them this way, but you can always type any variable name you want in there and click Insert. In this case, though, since the names are the same as your fields, you should be able to just insert the variables directly from the drop-down without typing their names first. Also, you probably already have each frame set up to call each variable name like "Barcode 1", "Barcode 2", etc., so you probably don't need to change those frames at all. That said, if you also have multiple text frames with those same names, you could do this instead: for (var i = 1; i <= 18; i++) { var fieldName = "Barcode " + i; var data = Field(fieldName); var barcode = ""; if (data) { data = Right("00000000000" + data, 11); // pad to 11 chars with leading zeros barcode = '<span font="IDAutomationUPCEANS">' + TaggedTextFromRaw(MakeUPCABarcode(data)) + '</span>'; } FindTextFrame(fieldName).content = barcode; } Here, instead of adding text variables, which can be called out in text frames, you're directly assigning the content of each text frame. Also, I haven't seen your template, but I suspect that you're simply outputting all these barcodes in some close arrangement on a page, in which case you could probably make a text rule that returns all of the barcodes and whatever other content you want, all together, and just use that single rule in a frame. Or maybe a repeatable component would be a better solution, or even a table. Again, though, without seeing the template, I'm kind of guessing. P.S. If you're coming to the Print show, I would be happy to do a hands-on with some of this stuff, either at the show itself or at the happy hour. 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.