Skip to main content
LynxKx
Inspiring
November 29, 2021
解決済み

Script to add a percent symbol to all numbers

  • November 29, 2021
  • 返信数 1.
  • 1232 ビュー

I'm trying to create a script in Illustrator that will find any numbers and add a percent symbol to the end. I tried deconstructing a grep chain script I wrote for InDesign and use it for Illustrator but its not working.

 

I'm basically trying add percents to my chart labels, If I ungroup my charts and use a script to add a percent symbol it will be all I need to finalize my charst fast!

このトピックへの返信は締め切られました。
解決に役立った回答 Manan Joshi

Wish I could edit this post... I figured out some script and it works to add the % labels, but its ignoring that I only want it done to numbers with the data_bars paragraph style. Any help would be appreciated!

var myDoc = app.activeDocument;
var frames = myDoc.textFrames;
var myStyle = myDoc.paragraphStyles.getByName("data_bars_right");

//add percentage to numbers with data style
for (var i = 0; i < frames.length; i++) {
    var text = frames[i].contents;
    if (!isNaN(text) && myStyle){
    frames[i].contents += "%";
}}
// Done
alert("Percentage labels added!");

 


Try the following

var myDoc = app.activeDocument;
var frames = myDoc.textFrames;

//add percentage to numbers with data style
for (var i = 0; i < frames.length; i++) {
    var text = frames[i].contents;
    if (!isNaN(text) && frames[i].paragraphs[0].paragraphStyles[0].name == "data_bars_right"){
		frames[i].contents += "%";
}}
// Done
alert("Percentage labels added!")

-Manan

返信数 1

pixxxelschubser
Community Expert
Community Expert
November 30, 2021

Please show what you have. And a dummy ai file and/or a screenshot.

LynxKx
LynxKx作成者
Inspiring
November 30, 2021

I created the chart with column designs. I was able to run a script to add ".0" to the whole numbers by ungrouping the charts first. I need to also callout a specific paragraph style "data_bars_right" so that the category labels don't change if they are numbers, like below. the javascript below is what i've been playing with.

 

main();

function main() {
	if (app.layoutWindows.length == 0) return;
	var changeObject = app.documents[0];
	if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
	var doc = app.documents[0];
	var style;
	var scriptVersion = app.scriptPreferences.version;
	app.scriptPreferences.version = 17.0;
	var options = app.findChangeGrepOptions.properties;
	app.findGrepPreferences = NothingEnum.NOTHING;
	app.changeGrepPreferences = NothingEnum.NOTHING;
	// Query [[Add Percent to Numbers]] -- If you delete this comment you break the update function
	try {
		app.findChangeGrepOptions.properties = ({includeFootnotes:true, widthSensitive:true});
		app.findGrepPreferences.properties = ({findWhat:"\\(?([\\d.,]+)\\)?"});
		app.changeGrepPreferences.properties = ({changeTo:"$1%"});
		changeObject.changeGrep();
	} catch (e) {alert(e + ' at line ' + e.line)}
	app.findChangeGrepOptions.properties = options;
	app.findGrepPreferences = NothingEnum.NOTHING;
	app.changeGrepPreferences = NothingEnum.NOTHING;
	app.scriptPreferences.version = scriptVersion;
};

function getStyleByString(docOrGroup, string, property) {
	if (string == 'data_bars_right') return docOrGroup[property][0];
	if (string == 'data_bars_cntrd') return docOrGroup[property][1];
	stringResult = string.match (/^(.*?[^\\]):(.*)$/);
	var styleName = (stringResult) ? stringResult[1] : string;
	styleName = styleName.replace (/\\:/g, ':');
	remainingString = (stringResult) ? stringResult[2] : '';
	var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
	var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
	if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
	return styleOrGroup;
};

 

LynxKx
LynxKx作成者
Inspiring
November 30, 2021

Wish I could edit this post... I figured out some script and it works to add the % labels, but its ignoring that I only want it done to numbers with the data_bars paragraph style. Any help would be appreciated!

var myDoc = app.activeDocument;
var frames = myDoc.textFrames;
var myStyle = myDoc.paragraphStyles.getByName("data_bars_right");

//add percentage to numbers with data style
for (var i = 0; i < frames.length; i++) {
    var text = frames[i].contents;
    if (!isNaN(text) && myStyle){
    frames[i].contents += "%";
}}
// Done
alert("Percentage labels added!");