Increase all prices by percentage
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Increase all prices by percentage
Tagged: percentage, prices, script
- This topic has 10 replies, 4 voices, and was last updated 7 years, 2 months ago by David Blatner.
-
AuthorPosts
-
-
January 23, 2017 at 2:45 am #91477dmanciniParticipant
Hi, I have to update a price list with prices formatted in this way:
€ 8,00
€ 25,00
€ 456,00
€ 1.543,00I need to increase the prices by 2% and round them to the biggest integer, so they should become:
€ 9,00
€ 26,00
€ 466,00
€ 1.564,00I’ve tried both Number Adjuster 1.2 and Price Adjuster scripts but without good results.
I’ve a paragraph style named “prezzo” assigned to any price. Is there a simple script to increase all numbers based on the paragraph style name? I’m running InDesign CS5.
Thanks in advance
-
January 23, 2017 at 2:59 am #91478Loic AigonMember
var main = function() {
var doc = app.properties.activeDocument;
if ( !doc ) return;
var fgp = app.findGrepPreferences.properties,
found = [], n, p, t;
app.findGrepPreferences = null;app.findGrepPreferences.properties = {
findWhat:”€\\s\\d{1,3}(\\.\\d{3})*(,\\d+)”,
appliedParagraphStyle:”prezzo”,
}found = doc.findGrep();
n = found.length;while ( n– ) {
t = found[n];
p = Number ( t.contents.replace ( /€\s+/, “”).replace ( /\./g, “” ).replace ( “,”, “.” ) );
t.contents = “€ “+String(p*1.02).replace ( /\./, “,” ).replace ( /(\d)(\d{3})/g, ‘$1.$2’ );
}
}var u;
app.doScript ( “main()”,u,u,UndoModes.ENTIRE_SCRIPT, “The Script” );
HTH
-
January 23, 2017 at 3:30 am #91480dmanciniParticipant
Many thanks for the quick reply!
It’s perfect except the numbers are not rounded to the greatest integer.
And the prices must always have two decimals (now they are removed if they are equal to 0).
PS: I’ve replaced all the curly quotes with straight quotes and also changed ( n– ) with ( n –)
-
January 23, 2017 at 4:17 am #91481dmanciniParticipant
Ok, when I wrote that I’ve changed ( n – ), I mean ( n minus minus ).
The minus minus is auto replaced by the forum engine with an en dash, that will cause a JS error if anyone would try the script.
-
January 23, 2017 at 4:35 am #91482Loic AigonMember
ok, I also noticed an issue with thousands:
var main = function() {
var doc = app.properties.activeDocument;
if ( !doc ) return;
var fgp = app.findGrepPreferences.properties,
found = [], n, p, t;
app.findGrepPreferences = null;app.findGrepPreferences.properties = {
findWhat:”€\\s\\d{1,3}(\\.\\d{3})*(,\\d+)”,
appliedParagraphStyle:”prezzo”,
}found = doc.findGrep();
n = found.length;while ( n– ) {
t = found[n];
p = Number ( t.contents.replace ( /€\s+/, “”).replace ( /\./g, “” ).replace ( “,”, “.” ) );
t.contents = “€ “+sepThousands( String(Math.floor(p*1.02)) )+ “,00″;
}
}function sepThousands ( contents ) {
var a = contents.split(‘.’);
var dec = a[1];
var integer = a[0].split(”);var na = [];
while ( sp = integer.splice ( -3, 3 ) ) {
na[na.length] = sp.join(”);
}return na.reverse().join(“.”)+(dec? ‘,’+dec : ”);
}var u;
app.doScript ( “main()”,u,u,UndoModes.ENTIRE_SCRIPT, “The Script” );
-
January 23, 2017 at 6:15 am #91483dmanciniParticipant
I’ve replaced Math.floor with Math.ceil to round to the biggest integer. So that 7,00 is rounded to 8,00 and not to 7,00.
It functions also on numbers bigger than thousand but the point that separates the thousands is still missing.
Many glyphs are substituted by the forum engine, can you please paste the script formatted as code to avoid this?
Thanks again
-
January 23, 2017 at 6:25 am #91484Masood AhmadParticipant
Loic, trying wrapping your code in…
< code > your code goes here </ code >
…just remove the space before and after the ‘code’ text to look like:
your code goes here
-
January 23, 2017 at 6:47 am #91486Loic AigonMember
< code >var main = function() {
var doc = app.properties.activeDocument;
if ( !doc ) return;
var fgp = app.findGrepPreferences.properties,
found = [], n, p, t;
app.findGrepPreferences = null;app.findGrepPreferences.properties = {
findWhat:”€\\s\\d{1,3}(\\.\\d{3})*(,\\d+)”,
appliedParagraphStyle:doc.paragraphStyles.itemByName(“prezzo”),
}found = doc.findGrep();
n = found.length;while ( n– ) {
t = found[n];
p = Number ( t.contents.replace ( /€\s+/, “”).replace ( /\./g, “” ).replace ( “,”, “.” ) );
t.contents = “€ “+sepThousands( String(Math.ceil(p*1.02)) )+ “,00″;
}
}function sepThousands ( contents ) {
var a = contents.split(‘.’);
var dec = a[1];
var integer = a[0].split(”);var na = [];
while ( sp = integer.splice ( -3, 3 ) ) {
na[na.length] = sp.join(”);
}return na.reverse().join(“.”)+(dec? ‘,’+dec : ”);
}var u;
app.doScript ( “main()”,u,u,UndoModes.ENTIRE_SCRIPT, “The Script” );
-
January 23, 2017 at 6:48 am #91487Loic AigonMember
Doesn’t seem to work :P
var main = function() {
var doc = app.properties.activeDocument;
if ( !doc ) return;
var fgp = app.findGrepPreferences.properties,
found = [], n, p, t;
app.findGrepPreferences = null;app.findGrepPreferences.properties = {
findWhat:"€\\s\\d{1,3}(\\.\\d{3})*(,\\d+)",
appliedParagraphStyle:doc.paragraphStyles.itemByName("prezzo"),
}found = doc.findGrep();
n = found.length;while ( n-- ) {
t = found[n];
p = Number ( t.contents.replace ( /€\s+/, "").replace ( /\./g, "" ).replace ( ",", "." ) );
t.contents = "€ "+sepThousands( String(Math.ceil(p*1.02)) )+ ",00";
}
}function sepThousands ( contents ) {
var a = contents.split('.');
var dec = a[1];
var integer = a[0].split('');var na = [];
while ( sp = integer.splice ( -3, 3 ) ) {
na[na.length] = sp.join('');
}return na.reverse().join(".")+(dec? ','+dec : '');
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" ); -
January 23, 2017 at 7:01 am #91488dmanciniParticipant
Great! Now it works perfectly!
-
January 23, 2017 at 7:08 am #91489David BlatnerKeymaster
Also, when the CODE html tag does not work, you can use PRE (I think PRE is often better, actually).
-
-
AuthorPosts
- You must be logged in to reply to this topic.