Copy link to clipboard
Copied
I have a document that's over 300 pages, containing tables that have columns of numbers representing measurement lengths in millimeters (mm). My client would like me to add corresponding US lengths (in inches, to two decimal places) next to each mm value, in parenthesis, and also in a smaller font size (if possible). For example, if a column in the table looks like this:
10
20
45
15
10
90
55
I need to end up with this:
10 (.39)
20 (.79)
45 (1.77)
15 (.59)
10 (.39)
90 (3.54)
55 (2.17)
Needless to say, for a 300 page document with tables on almost every page, it could take some time to do this all manually! Is there any script (or plugin) that would automate this process for me? Ideally the script (or plugin) would need to do both the mm to in. conversion, AND place the converted value next to the original value, in parenthesis. Any suggestions would be greatly appreciated! Thanks!
Copy link to clipboard
Copied
Hi,
Use below code and it will help you.
var myDoc = app.activeDocument;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '\\d+';
app.findGrepPreferences.appliedParagraphStyle = 'insert your paragraph style';//insert your paragraph style applied on digits
app.findGrepPreferences.appliedCharacterStyle = 'insert your character style';//insert your character style applied on digits or remove this line if no character style applied
var myFound = myDoc.findGrep();
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 10;
myFound.contents = myFound.contents + " (" + (parseInt(myFound.contents)*2) + ")"//replace your formula with "(parseInt(myFound.contents)*2)"
}
Shonky
Copy link to clipboard
Copied
Thanks for the response...unfortunately I'm a scripting newbie. I know how to get script code into a text file, save it out as java script, install it into the scripts panel folder, and run it in InDesign, but I'm no good with the coding part of it. I've decided to simplify (hopefully) my request. Forget about changing the font size, paragraph style, etc. All I need to do is take the current numerical value in the table cell, divide it by 25.4 (that's the conversion for mm to inches) and then display it (only to two decimal places) NEXT TO the original value (with a space after it), in parenthesis.
For example: current cell = 100, cell after running script on it = 100 (3.94).
If anyone can generate a script for this quickly and easily, they would earn tons of good karma points!
PS: I'm assuming that to run a script such as this, I would first select all text / table cells that I want to perform the script on, THEN run the script...is this correct?
Thanks again for any help!
Copy link to clipboard
Copied
wamboyil wrote:
PS: I'm assuming that to run a script such as this, I would first select all text / table cells that I want to perform the script on, THEN run the script...is this correct?
Thanks again for any help!
Its not compulsory that you select text and run script.
Script need some condition to search your digits.
1. What Paragraph Style applied on digits.
2. What Character Style applied on digits.
3. You want replace digits that appear in tables only.
4. Is any other digits appear in tables.
5. In which column your digits appear.
If you give details of above points then it will easy to catch your digits and replace with your desire result.
Shonky
Copy link to clipboard
Copied
Try to Use this code,
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\d+';
app.findGrepPreferences.appliedParagraphStyle = 'Your Para Style';
app.findGrepPreferences.appliedCharacterStyle = 'Your Char Style';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 10 ; //This will become 10 pt size of your change text
var myChange = myFound.contents + " (" + (parseInt(myFound.contents)/25.4)+ ")"
myFound.contents = myChange.slice (0, 8) + ")"
}
Mac
Copy link to clipboard
Copied
Thank you so much for your help. I have been working with this script and it's ALMOST working, here's how I have it so far:
var myDoc = app.activeDocument; app.findGrepPreferences = app.changeGrepPreferences = null; app.findGrepPreferences.findWhat = '
d+'; app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';app.findGrepPreferences.appliedCharacterStyle = 'Dimensions'; var myFound = myDoc.findGrep(); app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--){myFound.pointSize = 7 ; //This will become 7 pt size of your change text var myChange = myFound.contents + " (" + (parseInt(myFound.contents)/25.4)+ ")"myFound.contents = myChange.slice (0, 8) + ")"}
I adjusted the point size and added in my ParagraphStyle and CharacterStyle. I'm still having one small problem with the conversion. When the conversions are being made, I need to have the result displayed to two decimal points, for example, "270" should become "270 (10.63)". However, when I run the script, it displays as "270 (10.)", with nothing after the decimal point. Also, "180" should convert to "180 (7.09)" but it displays as "180 (7.0)". It appears that maybe the result is only showing two digits total, when I would like it to display as many total digits neccesary, but with two digits AFTER the decimal point. I hope this makes sense, and would appreciate another nudge in the right direction. Again, thanks for all your help so far on this!
Copy link to clipboard
Copied
Sorry, line spacing in my last post was bad...
Thank you so much for your help. I have been working with this script and it's ALMOST working, here's how I have it so far:
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = 'd+';
app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 7 ; //This will become 7 pt size of your change text
var myChange = myFound.contents + " (" + (parseInt(myFound.contents)/25.4)+ ")"
myFound.contents = myChange.slice (0, 8) + ")"
}
I adjusted the point size and added in my ParagraphStyle and CharacterStyle. I'm still having one small problem with the conversion. When the conversions are being made, I need to have the result displayed to two decimal points, for example, "270" should become "270 (10.63)". However, when I run the script, it displays as "270 (10.)", with nothing after the decimal point. Also, "180" should convert to "180 (7.09)" but it displays as "180 (7.0)". It appears that maybe the result is only showing two digits total, when I would like it to display as many total digits neccesary, but with two digits AFTER the decimal point. I hope this makes sense, and would appreciate another nudge in the right direction. Again, thanks for all your help so far on this!
Copy link to clipboard
Copied
If you want to round to decimal places use Math object e.g.
var x = 270 / 25.4
$.writeln(x);
var z = roundToDP(x, 2)
$.writeln(z);
function roundToDP(number, decPlaces) {
// Decimal = base 10
dpNumber = Math.round(number * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
return dpNumber;
}
Copy link to clipboard
Copied
Mark, that does not return any trailing zeroes. Try this: multiply by the required number of tens to get rid of the actual decimal point; then round off to get rid of anything beyond that. Convert to string; sneak in a decimal point at the correct place.
val = prompt ("Any value. Any one!", );
var z = roundToDP(Number(val), 2)
alert (z);
function roundToDP (val,dig)
{
// Including all its zeros, lopping off beyond:
val = Math.round(val*Math.pow(10,dig));
// Force in decimal point at the right place:
val = String(val);
// return val.slice(0,val.length-dig)+"."+val.slice(dig);
return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
}
[Ed.] Oops -- didn't work for "5" to "5.00". Now it does
Copy link to clipboard
Copied
Well spotted Jongware didn't see that… This in AppleScript always made me
set x to round 270 / 25.4 rounding as taught in school
Copy link to clipboard
Copied
I really do appreciate all the help being given! However, I have to hang my head in shame and admit again to being a scripting newbie. In the last few comments regarding the decimal point rounding, where would I insert those lines into my current script (In comment #6 above)? I'm thinking that this will be the final piece to my puzzle, and will make my life a lot happier! Thanks again!!!
Copy link to clipboard
Copied
At a (very!) fast glance, you only have to replace this
var myChange = myFound.contents + " (" + (parseInt(myFound.contents)/25.4)+ ")"
myFound.contents = myChange.slice (0, 8) + ")"
by this
myChange = myFound.contents+" ("+roundToDP( parseInt(myFound.contents), 2)+")";
myFound.contents = myChange;
-- and don't forget to include the roundToDP function at the very bottom of your script. JS is smart enough to see it's a function, so it won't try and execute it when encountered after your loop.
(What's up with the 'slice' in your 2nd line? Was it an attempt to cut off unwanted stuff? If so, it's not necessary anymore )
Copy link to clipboard
Copied
Here is final version:
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\d+';
app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 7 ; //This will become 7 pt size of your change text
var myChange = myFound.contents + " (" + roundToDP ((parseInt(myFound.contents)/25.4),2)+ ")"
myFound.contents = myChange
}
function roundToDP (val,dig)
{
// Including all its zeros, lopping off beyond:
val = Math.round(val*Math.pow(10,dig));
// Force in decimal point at the right place:
val = String(val);
// return val.slice(0,val.length-dig)+"."+val.slice(dig);
return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
}
Shonky
Copy link to clipboard
Copied
Thanks once again for all the help, I tried the latest version provided by Shonkyin, but now when I select a value and run the script on it, nothing happens. I get no error message, and nothing is added or changed at all. Also, now it appears that the client only wants the results to be shown rounded to 1 decimal point...what needs to change in the script in order to make this adjustment?
Thanks thanks thanks thanks!
Copy link to clipboard
Copied
Does anyone have any suggestions for this issue yet? Much thanks in advance to anyone who can help me!
Copy link to clipboard
Copied
Hi Dear,
Above posted script working very well and if its not working at your end I am sure that there is some problem with applied paragraph and character style name. Please check it carefully.
And i have change decimal to 1 in below script.
var myDoc = app.activeDocument;
var result = 0;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\d+';
app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 7 ; //This will become 7 pt size of your change text
var myChange = myFound.contents + " (" + roundToDP ((parseInt(myFound.contents)/25.4),1) + ")"
myFound.contents = myChange
result ++
}
function roundToDP (val,dig)
{
// Including all its zeros, lopping off beyond:
val = Math.round(val*Math.pow(10,dig));
// Force in decimal point at the right place:
val = String(val);
// return val.slice(0,val.length-dig)+"."+val.slice(dig);
return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
}
alert (result + " metric converted... :)")
Shonky
Copy link to clipboard
Copied
Thanks so much, this works perfectly now! (I did have a little glitch with the character style naming.) Thanks again to everyone for their help.
Copy link to clipboard
Copied
Well, here I am again with another SMALL (hopefully) request. Now that I've got the script working perfectly (see above), I need to make a similar script, exactly the same as above, but instead of the mm to in conversion (divide by 25.4) I need to convert kg to lbs (multiply by 2.2). All the same criteria as before (result displays next to the original value, in parentheses, rounded to one decimal point). I thought I could figure this out myself, and in the script above, I replaced "/25.4" with "*2.2", but I was getting strange results, the rounded decimal pojnt wasn't correct in most cases, and in cases where the original value had a decimal point in it (it will only ever be one decimal point, if there is one), the result was really messed up. I thought the simple replacement of the functions would work, but I guess not. Help, one more time, please?
Copy link to clipboard
Copied
Above script showing wrong value if you have decimal in your digits because above grep search only for digits. I have corrected in below script. You getting last decimal wrong because you multiply with 2.2 this is not exact value. see in below script correct one.
var myDoc = app.activeDocument;
var result = 0;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '[\\d\.]+';
app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 7 ; //This will become 7 pt size of your change text
var myChange = myFound.contents + " (" + roundToDP ((parseInt(myFound.contents)*2.20462262),1) + ")"
myFound.contents = myChange
result ++
}
function roundToDP (val,dig)
{
// Including all its zeros, lopping off beyond:
val = Math.round(val*Math.pow(10,dig));
// Force in decimal point at the right place:
val = String(val);
// return val.slice(0,val.length-dig)+"."+val.slice(dig);
return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
}
alert (result + " lbs converted... :)")
Shonky
Copy link to clipboard
Copied
Thanks once again for responding so quickly. When I use this script, it only works correctly when the original value is a whole number (with no decimals in it). However, when the original value has a decimal in it, the converted value is incorrect (according to a manual conversion program I have been using). For example, a value of 74 returns a converted value of 163.1 (which is correct), but a value of 74.5 also returns a value of 163.1, when it should actually be 164.2. Could it be that any digit after the original decimal point is being ignored in the conversion?
Copy link to clipboard
Copied
Even before that. Shonky thoughtfully changed the FindGrep expression to search for a decimal separator, but overlooked that
parseInt(myFound.contents) [...]
does what the name says: make an integer of whatever is put in there. (In case you are wondering: an integer, in computer terms, is always a whole number -- no decimals allowed. For most programming languages, there is also a limit in allowed numbers -- only the biggest number that "fits" in the command processor in one go. I wouldn't know how that works for Javascript, though. End of side note.)
So instead of parseInt, you would need another function to force Javascript to treat the found text contents (which is a String) as a number. You can try this:
Number(myFound.contents)
but be warned, this one will fail on anything that cannot be converted to a real number. You might expect it will not, because the GREP search -- which feeds it with its results -- only searches for digits and a decimal period. Well, no. As it is, this "number" 12.34.56.78 will be found in its entirety, fed into the Number function, and it'll fail. Best is to change the GREP string to something that avoids the issue entirely, something like
app.findGrepPreferences.findWhat = '\\d*(\\.\\d+)?';
-- which will search for zero or more digits, then optionally followed by a period and one or more digits. I think this will work for anything from .0001 to 1 to 11111.
Copy link to clipboard
Copied
I tried adding the two lines suggested by Jongware, but I'm still getting the same results (correct result when the original value is a whole number, and incorrect result when the original value has a decimal point in it). Here is what the script looks like currently:
var myDoc = app.activeDocument;
var result = 0;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\d*(\\.\\d+)?'
app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
Number(myFound.contents)
myFound.pointSize = 7 ; //This will become 7 pt size of your change text
var myChange = myFound.contents + " (" + roundToDP ((parseInt(myFound.contents)*2.20462262),1) + ")"
myFound.contents = myChange
result ++
}
function roundToDP (val,dig)
{
// Including all its zeros, lopping off beyond:
val = Math.round(val*Math.pow(10,dig));
// Force in decimal point at the right place:
val = String(val);
// return val.slice(0,val.length-dig)+"."+val.slice(dig);
return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
}
Copy link to clipboard
Copied
Check this one but if you have decimal like 5.7.6 then it will give you wrong result and i hoping that you have not decimal like this.
var myDoc = app.activeDocument;
var result = 0;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+'
app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for (a=myFound.length-1; a>=0; a--)
{
myFound.pointSize = 7 ; //This will become 7 pt size of your change text
var myChange = myFound.contents + " (" + roundToDP ((Number(myFound.contents)*2.20462262),1) + ")"
myFound.contents = myChange
result ++
}
function roundToDP (val,dig)
{
// Including all its zeros, lopping off beyond:
val = Math.round(val*Math.pow(10,dig));
// Force in decimal point at the right place:
val = String(val);
// return val.slice(0,val.length-dig)+"."+val.slice(dig);
return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
}
Note: above grep not work in all case. So please be careful when using it.
Copy link to clipboard
Copied
Thank you so much, this works perfectly now!
Copy link to clipboard
Copied
Hello to everyone on the boards once again. I was assisted by the helpful people here a few months ago with a scripting question, and I have another question. The last version of the script I am working with is shown in post #22 above. I am converting tables full of metric dimensional values into US dimensions, in this case kg to pounds. In the latest version of the script I am using (post #22), I am working with kg values that have one decimal place in them and I am converting them into lbs values that have one decimal place in them.
My current need is to have this script modified to be able to work with kg values that have TWO decimal places in them and convert them into lbs values that still only have ONE decimal place in them. (Ideally I would like the same script to work on kg values that are zero, one OR two decimal places, and convert any of them to lbs with one decimal place.) I tried the current script on values with two decimal places in them, and it was not working. Can someone take a look at the script in post #22 and let me know how I need to modify it to be able to work with zero, one or two decimal places.
Thanks again so much!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now