#1
|
|||
|
|||
![]()
I just had a job that was a coupon where each card had a unique code, and they wanted the job delivered in multiple sets of differing quantities. Here is the code I used to successfully produce the job:
Code:
// 4 sets of 380 if ((CurrentRecordNumber() >= 1) && (CurrentRecordNumber() <= 1520)) { SetQuantity = 380 } // 9 sets of 350 else if ((CurrentRecordNumber() >= 1521) && (CurrentRecordNumber() <= 4670)) { SetQuantity = 350 } // 34 sets of 255 else if ((CurrentRecordNumber() >= 4671) && (CurrentRecordNumber() <= 13340)) { SetQuantity = 255 } // 26 sets of 215 else if ((CurrentRecordNumber() >= 13341) && (CurrentRecordNumber() <= 18930)) { SetQuantity = 215 } // 28 sets of 170 else if ((CurrentRecordNumber() >= 18931) && (CurrentRecordNumber() <= 23690)) { SetQuantity = 170 } // 11 sets of 115 else if ((CurrentRecordNumber() >= 23691) && (CurrentRecordNumber() <= 24955)) { SetQuantity = 115 } // 1 set of 45 else if ((CurrentRecordNumber() >= 24956) && (CurrentRecordNumber() <= 25000)) { SetQuantity = 45 } if (SetCounter == 1) { FusionPro.Composition.SetBodyPageUsage('divider', true) } if (SetCounter < SetQuantity) { SetCounter++; } else { SetCounter = 1; } Is there a way to make this more concise, and to determine the record numbers in a more dynamic way? I see this as being a repeating job in the future, but with differing set quantities and amounts per set. It would be nice to just enter the number of sets and quantities per set and have code to figure out the record numbers. I would appreciate any guidance anyone is willing to share! Thank you... |
#2
|
||||
|
||||
![]()
Great question! Sure, this logic can be reduced in several ways.
First, since you're in an "if...else" chain, you know that each block is gated by the one before. For instance, if the number is less than 1521, you'll go to the first case, so that next "if" doesn't need to re-check that it's less that 1521 again, and so on. And you don't need to check that the number is at least one at the start; it always is, by definition. You can also get rid of those braces since each statement block is just a single statement. Also, you probably want a final "else" at the end, just in case. So, skipping the "SetCounter" logic at the end, the first reduction is: Code:
// 4 sets of 380 if (CurrentRecordNumber() <= 1520) SetQuantity = 380 // 9 sets of 350 else if (CurrentRecordNumber() <= 4670) SetQuantity = 350 // 34 sets of 255 else if (CurrentRecordNumber() <= 13340) SetQuantity = 255 // 26 sets of 215 else if (CurrentRecordNumber() <= 18930) SetQuantity = 215 // 28 sets of 170 else if (CurrentRecordNumber() <= 23690) SetQuantity = 170 // 11 sets of 115 else if (CurrentRecordNumber() <= 24955) SetQuantity = 115 // 1 set of 45 else if (CurrentRecordNumber() <= 25000) SetQuantity = 45 else SetQuantity = 30 Code:
var recNum = CurrentRecordNumber(); recNum = 5000; // debugging // 4 sets of 380 if (recNum <= 1520) SetQuantity = 380 // 9 sets of 350 else if (recNum <= 4670) SetQuantity = 350 // 34 sets of 255 else if (recNum <= 13340) SetQuantity = 255 // 26 sets of 215 else if (recNum <= 18930) SetQuantity = 215 // 28 sets of 170 else if (recNum <= 23690) SetQuantity = 170 // 11 sets of 115 else if (recNum <= 24955) SetQuantity = 115 // 1 set of 45 else if (recNum <= 25000) SetQuantity = 45 else SetQuantity = 30 return [recNum, SetQuantity]; // debugging Now we can try other ways of reducing this code. There are probably different schools of thought about this, but here's where I would land: Code:
var recNum = CurrentRecordNumber(); //recNum = 20000; // debugging var Quantities = { 1520: 380, // 4 sets of 380 4670: 350, // 9 sets of 350 13340: 255, // 34 sets of 255 18930: 215, // 26 sets of 215 23690: 170, // 28 sets of 170 24955: 115, // 11 sets of 115 25000: 45, // 1 set of 45 0: 30 // default }; for (var q in Quantities) { if (recNum <= q || q == 0) { SetQuantity = Quantities[q]; break; } } //return [recNum, SetQuantity]; // debugging
__________________
Dan Korn FusionPro Developer / JavaScript Guru / Forum Moderator PTI Marketing Technologies | Printable | MarcomCentral I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com. Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs. This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data. Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages. Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance. Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support. How To Ask Questions The Smart Way The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript. Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5. return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)}); ![]() Last edited by Dan Korn; May 28th, 2021 at 02:22 PM.. |
#3
|
|||
|
|||
![]()
As always Dan, thank you! Your insights and knowledge are immensely appreciated.
|
![]() |
Thread Tools | Search this Thread |
Display Modes | |
|
|