• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help with this Script for US currency formatting

Community Beginner ,
Nov 22, 2023 Nov 22, 2023

Copy link to clipboard

Copied

This is the task I've been assigned:

Create a script that will take numbers from certain data fields and format them into US currency. This action is to trigger after all variable fields have been populated. For example, this:

Figure A.

AaroenAMS_0-1700672853564.png

to this:

Figure B.

AaroenAMS_1-1700672989857.png

 

I'm not a coder. As such I've turn to AI tools to help me along. So far I've gotten this far:

Figure C.

AaroenAMS_2-1700673196065.png

This is my working code that's getting me figure C results:

Figure D.

// Check if InDesign application is running
if (app && app.name === "Adobe InDesign" && app.version >= "18.5") {
  // Define the GREP Find/Change parameters
  var findGrepOptions = app.findGrepPreferences;
  var changeGrepOptions = app.changeGrepPreferences;

  // Define the regular expression pattern to match numbers with optional commas and currency symbols
  var currencyPattern = "[$,]";
  var numberPattern = "\\b(\\d+(\\.\\d{2})?)\\b"; // Matches numbers with optional two decimal places, using word boundaries

  // Set the find and change properties to strip out commas and currency symbols
  findGrepOptions.findWhat = currencyPattern;
  changeGrepOptions.changeTo = "";

  // Perform the Find/Change operation to strip out commas and currency symbols
  app.activeDocument.changeGrep();

  // Set the find properties for numbers to be converted to US currency format
  findGrepOptions.findWhat = numberPattern;

  // Find all instances of the numbers
  var foundItems = app.activeDocument.findGrep();

  // Define a function to ignore numbers already formatted properly
  function isFormattedProperly(match) {
    return match.toString().indexOf("$") === -1; // Ignore if already formatted as US currency
  }

  // Loop through found items and change them to US currency format
  for (var i = 0; i < foundItems.length; i++) {
    if (isFormattedProperly(foundItems[i])) {
      foundItems[i].contents = "$" + Number(foundItems[i].contents).toFixed(2);
    }
  }

  // Reset the Find/Change preferences
  findGrepOptions = NothingEnum.NOTHING;
  changeGrepOptions = NothingEnum.NOTHING;

  alert("Number formats have been converted to US Currency format.");
} else {
  alert("Adobe InDesign 18.5 or later is required to run this script.");
}

  

So what do I need to change so I can get the results of figure A?

 

Any help is greatly appreciated.

TOPICS
Scripting

Views

48

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2023 Nov 22, 2023

Copy link to clipboard

Copied

LATEST

Not a scripter, but from other coding experience you may need to 'escape' the commas so they aren't interpreted as code, but as content.


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines