Increase all prices by percentage

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Increase all prices by percentage

Viewing 10 reply threads
  • Author
    Posts
    • #91477
      dmancini
      Participant

      Hi, I have to update a price list with prices formatted in this way:

      € 8,00
      € 25,00
      € 456,00
      € 1.543,00

      I 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,00

      I’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

    • #91478
      Loic Aigon
      Member

      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

      Loic
      http://www.ozalto.com

    • #91480
      dmancini
      Participant

      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 –)

    • #91481
      dmancini
      Participant

      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.

    • #91482
      Loic Aigon
      Member

      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” );

    • #91483
      dmancini
      Participant

      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

    • #91484
      Masood Ahmad
      Participant

      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

    • #91486
      Loic Aigon
      Member

      < 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” );

    • #91487
      Loic Aigon
      Member

      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" );

    • #91488
      dmancini
      Participant

      Great! Now it works perfectly!

    • #91489
      David Blatner
      Keymaster

      Also, when the CODE html tag does not work, you can use PRE (I think PRE is often better, actually).

Viewing 10 reply threads
  • You must be logged in to reply to this topic.
>