cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
terapin
Level VI

JSL code to clear selected data table contents

I'm trying to do something that I think should be very easy to accomplish, but I can't find the JSL instruction that will let me.  Basically, after selecting various columns and rows I want to delete the contents of the selected cells, not the entire column or row.  I can do this manually, but can't find the JSL command to do so.  Any suggestions would be appreciated.

 

E.g.,

 

 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << Select Rows( [5, 7, 8, 10] );
Column ( "name" ) << Set Selected ( 1 );
Column ( "sex" ) << Set Selected ( 1 );

 

 

Now delete the contents of the selected cells.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
ian_jmp
Staff

Re: JSL code to clear selected data table contents

Something like this:

 

 

NamesDefaultToHere(1);
 
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
 
// Set up a selection of cells
dt << Select Rows( [5, 7, 8, 10] );
Column (dt, "name" ) << Set Selected ( 1 );
Column (dt, "sex" ) << Set Selected ( 1 );
 
// Function to set currently selected cells to '.' or ""
clearSelectedCells =
  Function({dt}, {Default Local},
  selCols = dt << getSelectedColumns;
  selRows = dt << getSelectedRows;
  for(c=1, c<=NItems(selCols), c++,
  dType = Column(dt, selCols[c]) << getDataType;
  if (dType == "Numeric",
  Column(dt, selCols[c])[selRows] = .
  ,
  Column(dt, selCols[c])[selRows] = ""
  );
  );
  );
 
// Use the function
clearSelectedCells(dt);

 

View solution in original post

6 REPLIES 6
ian_jmp
Staff

Re: JSL code to clear selected data table contents

Something like this:

 

 

NamesDefaultToHere(1);
 
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
 
// Set up a selection of cells
dt << Select Rows( [5, 7, 8, 10] );
Column (dt, "name" ) << Set Selected ( 1 );
Column (dt, "sex" ) << Set Selected ( 1 );
 
// Function to set currently selected cells to '.' or ""
clearSelectedCells =
  Function({dt}, {Default Local},
  selCols = dt << getSelectedColumns;
  selRows = dt << getSelectedRows;
  for(c=1, c<=NItems(selCols), c++,
  dType = Column(dt, selCols[c]) << getDataType;
  if (dType == "Numeric",
  Column(dt, selCols[c])[selRows] = .
  ,
  Column(dt, selCols[c])[selRows] = ""
  );
  );
  );
 
// Use the function
clearSelectedCells(dt);

 

ms
Super User (Alumni) ms
Super User (Alumni)

Re: JSL code to clear selected data table contents

Nice function Ian! To simplify it further you could avoid treating different data types separately by using Empty(). It works for both Numeric and Character columns.

 

 

For(c = 1, c <= N Items(selCols), c++, Column(dt, selCols[c])[selRows] = Empty());
ian_jmp
Staff

Re: JSL code to clear selected data table contents

Many thanks! Fewer characters is good, especially with my typing . . .

ms
Super User (Alumni) ms
Super User (Alumni)

Re: JSL code to clear selected data table contents

A simple but potentially risky method is to use MainMenu() to invoke the Delete command in the Edit Menu. It is risky because it will delete anything that is selected in the active window. So in a complex script, make sure to keep track of what window that will be on top when the command is run.

 

 

dt = Open("$SAMPLE_DATA/Big Class.jmp");
dt << Select Rows([5, 7, 8, 10]);
Column("name") << Set Selected(1);
Column("height") << Set Selected(1);
 
 
dt << Bring Window To Front;
Main Menu("Delete");

 

 

 

terapin
Level VI

Re: JSL code to clear selected data table contents

Thanks folks for your suggestions,

Nice to see several options.  I was thinking there should be a typical command I could call as suggested by MS ( Note: actual command is  Main Menu("Clear");, not Main Menu("Delete"))


Jeff_Perkinson
Community Manager Community Manager

Re: JSL code to clear selected data table contents

There's a difference between operating system standards here.  Windows uses Edit->Clear and the Mac uses Edit->Delete.

So, to make a script robust to either you'll need to use:


If(Host is( Windows ),


  Main Menu( "Clear" ), //Windows


  Main Menu( "Delete" ) //Mac


);


-Jeff

-Jeff