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

How to Make Symbols Appear in Superscript?

Enthusiast ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

Hi Experts, 

I Wrote Small Script for Prefixing numbers via GREP in Javascript, but i wish that symbol will appear in superscript , can this be done?

 

var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
  exit(0);
}

function DoGrepPrefix(){
        var Selectedcells = app.selection[0];
        myChoice = myDropdown.selection;
        String(myChoice).position = Position.SUPERSCRIPT;
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        //
        app.findGrepPreferences.findWhat="\\d+";
        app.changeGrepPreferences.changeTo= String(myChoice)+"$0"; //Prefix    
        Selectedcells.findGrep();
        Selectedcells.changeGrep();
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
    }

DoGrepPrefix();

 

 the script work normal with no error but doesn't execute command in this line :

 

String(myChoice).position = Position.SUPERSCRIPT;

 

So the final text not appear in superscript, i know that's can be done form find manually but i hope it can be done by the script for more fast result, and thanks in advance

Best
Mohammad Hasanin
TOPICS
Scripting

Views

314

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 ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

 

 

app.changeGrepPreferences.position = Position.SUPERSCRIPT;

 

Add this before your changeGrep() call. Not sure why you're calling findGrep() as well. String.position is not a property. I don't know why this code is not throwing an error. 

Edit. If you just want the currency character to be superscript then do this: 

 

function DoGrepPrefix(){
        var Selectedcells = app.selection[0];
        myChoice = myDropdown.selection;
        //String(myChoice).position = Position.SUPERSCRIPT;
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        //here we change the findGrep to just find the insertion point before the digits
        app.findGrepPreferences.findWhat="(?=\\d+)";
        app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
        app.changeGrepPreferences.position= Position.SUPERSCRIPT;    
        //Selectedcells.findGrep();
        Selectedcells.changeGrep();
        app.findGrepPreferences.findWhat="^(?=\\d+)";
        Selectedcells.changeGrep();

        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
    }

 

 

 

 

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
Enthusiast ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

Hi @brianp311 , Thanks for your reply, but when i updated the script it doesnt work at all also no error!

var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
  exit(0);
}

function DoGrepPrefix(){
        var Selectedcells = app.selection[0];
        myChoice = myDropdown.selection;
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        //here we change the findGrep to just find the insertion point before the digits
        app.findGrepPreferences.findWhat="(?=\\d+)";
        app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
        app.changeGrepPreferences.position= Position.SUPERSCRIPT;    
        Selectedcells.changeGrep();
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
    }

DoGrepPrefix();
Best
Mohammad Hasanin

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
Community Expert ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

If the number begins the cell then you need to put a ^ in front of your lookahead: 

 

"^(?=\\d+)"

 

You might want to do two searches to catch both, unless someone more in-tune with GREP has a better solution. Edited the code above to do both finds and replaces.

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
Enthusiast ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

Hi @brianp311 after modifying the GREP Code the Script Worked! but also no change for the currency symbol, its in the same position and no superscript position :

var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
  exit(0);
}

function DoGrepPrefix(){
        var Selectedcells = app.selection[0];
        myChoice = myDropdown.selection;
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        //here we change the findGrep to just find the insertion point before the digits
        app.findGrepPreferences.findWhat="^(?=\\d+)";
        app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
        app.changeGrepPreferences.position= Position.SUPERSCRIPT;    
        Selectedcells.changeGrep();
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
    }

DoGrepPrefix();
Best
Mohammad Hasanin

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
Enthusiast ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

@brianp311 Thank you , I Will Try to make another function and see result

Best
Mohammad Hasanin

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
Enthusiast ,
Dec 10, 2021 Dec 10, 2021

Copy link to clipboard

Copied

it works with all other symbols except dollar sign ($), thats weird! 

 

var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
  exit(0);
}

function DoGrepPrefix(){
        var Selectedcells = app.selection[0];
        myChoice = myDropdown.selection;
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        //here we change the findGrep to just find the insertion point before the digits
        app.findGrepPreferences.findWhat="^(?=\\d+)";
        app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
        app.changeGrepPreferences.position= Position.SUPERSCRIPT;    
        Selectedcells.changeGrep();
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        CallDoGrepSuperscript(); //Call Second Search!
    }

DoGrepPrefix();

function CallDoGrepSuperscript(){ //Second Search
        var Selectedcells = app.selection[0];
        myChoice = myDropdown.selection;
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        app.findGrepPreferences.findWhat=String(myChoice); //Prefix
        app.changeGrepPreferences.position= Position.SUPERSCRIPT;    
        Selectedcells.changeGrep();
        //
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
    }

 

Best
Mohammad Hasanin

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
Community Expert ,
Dec 11, 2021 Dec 11, 2021

Copy link to clipboard

Copied

Hi @M.Hasanin, problem might be because $ is a regular expression special symbol. I haven't tested, but escaping the $ might work. Something like:

app.changeGrepPreferences.changeTo= String(myChoice).replace('$','\$');

 - Mark

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
Enthusiast ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

Thanks a lot @m1b it works in findWhat in the second function :

app.findGrepPreferences.findWhat=String(myChoice).replace('$','\\$');

 

Best
Mohammad Hasanin

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
Community Expert ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

LATEST

Excellent! Well done working that out! Some cases you need to escape the dollarsign, but other cases you need to escape the backslash!

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