Come faccio a salvare una tabella come nome di una variabile, dato l'anno dei dati?Ad esempio: ho due file excel: 2014.xlsx, 2015.xlsx e vorrei automatizzarli dopo aver selezionato questi file multipli per denominare specifici fogli di lavoro all'interno di questi file (in questo caso {Product Events, Products, CodesFrmPLI}), per un totale di 6 diverse tabelle JMP: gli esempi di nomi sono Product_Events_2014, Product_Events_2015, Products_2014, Products_2015, CodesFrmPLI_2014, CodesFrmPLI_2015 per questo caso.
Names Default To Here( 1 );
Close All( data tables );
Delete Symbols();
Directory = Pick Directory( "Select a directory" );
Files = Pick File(
"Select Data Set(s)",
Directory,
{"All Files|*", "JMP Files|jmp;jsl;jrn", "Excel Files|xlsx"},
1,
0,
"",
"multiple"
);
For( i = 1, i <= N Items( Files ), i++,
Try( Open( Files[i],
Worksheets( NWorksheet = {"Product Events", "Products", "CodesFrmPLI"}),
Use for all sheets( 1 ),
Concatenate Worksheets( 0 ),
Create Concatenation Column( 0 ),
Worksheet Settings(
1,
Has Column Headers( 1 ),
Number of Rows in Headers( 1 ),
Headers Start on Row( 1 ),
Data Starts on Row( 2 ),
Data Starts on Column( 1 ),
Data Ends on Row( 0 ),
Data Ends on Column( 0 ),
Replicated Spanned Rows( 1 ),
Replicated Spanned Headers( 0 ),
Suppress Hidden Rows( 1 ),
Suppress Hidden Columns( 1 ),
Suppress Empty Columns( 1 ),
Treat as Hierarchy( 1 ),
Multiple Series Stack( 0 ),
Import Cell Colors( 0 ),
Limit Column Detect( 0 ),
Column Separator String( "-" ),
invisible
) )
));
Show(NWorksheet);
//*** 2014 Naming convention
For( i = 1, i <= N Items( Files ), i++,
if( Contains( Files[i] , "2014") > 0,
dt = Data Table(1);
dt << Save("Codes_2014.jmp");
dt = Data Table(2);
dt << Save("Products_2014.jmp");
dt = Data Table(3);
dt << Save("Product Events_2014.jmp");
//Close All(Data Tables, No Save);
)
);
//*** 2015 Naming convention
For( i = 1, i <= N Items( Files ), i++,
if( Contains( Files[i] , "2015") > 0,
dt = Data Table(4);
dt << Save("Codes_2015.jmp");
dt = Data Table(5);
dt << Save("Products_2015.jmp");
dt = Data Table(6);
dt << Save("Product Events_2015.jmp");
//Close All(Data Tables, No Save);
)
);Ri: Come faccio a salvare una tabella come nome di una variabile, dato l'anno dei dati?Penso che la rielaborazione di sotto del tuo script ti porterà dove vuoi ....... Non avevo un ambiente di test molto esteso, ma penso che il codice sia abbastanza vicino a quello che ti serve:
Names Default To Here( 1 );
Close All( data tables );
Delete Symbols();
Directory = Pick Directory( "Select a directory" );
Files = Pick File(
"Select Data Set(s)",
Directory,
{"All Files|*", "JMP Files|jmp;jsl;jrn", "Excel Files|xlsx"},
1,
0,
"",
"multiple"
);
// Place the names of the spreadsheets to be input into a list
spreadsheetList = {"Product Events", "Products", "CodesFrmPLI"};
For( i = 1, i <= N Items( Files ), i++,
// For this file, determine the year
If(
Contains( Files[i], "2014" ), theYear = "2014",
Contains( Files[i], "2015" ), theYear = "2015"
);
// Read in separately each separate spreadsheet
For( k = 1, k <= N Items( spreadsheetList ), k++,
// Create a list that will contain the current spreadsheet to
// be read in
extractList = {};
Insert Into( extractList, spreadsheetList[k] );
// Open the spreadsheet
Try(
dt = Open(
Files[i],
Worksheets( NWorksheet = extractList ),
Use for all sheets( 1 ),
Concatenate Worksheets( 0 ),
Create Concatenation Column( 0 ),
Worksheet Settings(
1,
Has Column Headers( 1 ),
Number of Rows in Headers( 1 ),
Headers Start on Row( 1 ),
Data Starts on Row( 2 ),
Data Starts on Column( 1 ),
Data Ends on Row( 0 ),
Data Ends on Column( 0 ),
Replicated Spanned Rows( 1 ),
Replicated Spanned Headers( 0 ),
Suppress Hidden Rows( 1 ),
Suppress Hidden Columns( 1 ),
Suppress Empty Columns( 1 ),
Treat as Hierarchy( 1 ),
Multiple Series Stack( 0 ),
Import Cell Colors( 0 ),
Limit Column Detect( 0 ),
Column Separator String( "-" ),
invisible
)
)
);
// The name for the JMP file is the same as the spreadsheet name,
// except for the Codes, so make the change
If( extractList[1] == "CodesFrmPLI",
extractList[1] = "Codes"
);
// Save the JMP file with the prescribed name
close( dt, save( extractList[1] || "_" || theYear || ".jmp" ) );
);
);
Ri: Come faccio a salvare una tabella come nome di una variabile, dato l'anno dei dati?Grazie Jim. Questo ha funzionato. Quick follow-up: se mi piacerebbe ora eseguire l'unione di tabelle e la manipolazione dei dati, dove sarebbe il posto migliore per questo?
Saluti.
Ri: Come faccio a salvare una tabella come nome di una variabile, dato l'anno dei dati?Se rimuovi la linea
close( dt, save( extractList[1] || "_" || theYear || ".jmp" ) );
Le tabelle di dati rimarranno aperte. Pertanto, è possibile aggiungere eventuali join o calcoli desiderati dopo la chiusura ")" per il ciclo For () iniziale. In sostanza, dopo l'ultima riga nello script corrente.