Difference between revisions of "Read and Write Spreadsheets"

 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This page goes over the following example model on how to read and write to spreadsheets in desktop Analytica. See [[Spreadsheets in ACP]] to see the parallel example model for ACP.
+
[[Category:Excel to Analytica mappings]]
*[[Media:SpreadsheetExampleDTA.zip| Example Model & Spreadsheet]]
 
  
[[File:SpreadsheetExampleDTA.jpg]]
+
__TOC__
  
In the example model, the '''Read Spreadsheet''' button will prompt a user to select a workbook and copy the contents of that workbook into the '''Saved Data''' node. The '''Save Results to Spreadsheet''' button will copy the results of the '''Miles per Year''' node into the original workbook file, and save that as a new workbook file named "saved_example".
+
 
 +
This page goes over the following example model on how to read and write to spreadsheets in desktop Analytica. <!--See [[Spreadsheets in ACP]] to see the parallel example model for [[Analytica Cloud Player]] ACP.-->
 +
 
 +
*[[Media:SpreadsheetExampleDTA.zip| Download Example Model & Spreadsheet]]
 +
 
 +
:[[File:SpreadsheetExamplePicDTA.jpg|425px]]
 +
 
 +
In the example model, the '''Read Spreadsheet''' button prompts a user to select a workbook and copies the contents of that workbook into the <code>Saved Data</code> node. The '''Save Results to Spreadsheet''' button copies the results of the <code>Miles per Year</code> node into the original workbook file, and saves that as a new workbook file named <code>saved_example</code>.
  
 
==Reading Spreadsheets==
 
==Reading Spreadsheets==
To read a spreadsheet in Analytica, the user must first open a workbook using SpreadsheetOpen(). Then The user can use SpreadsheetRange() to determine what to import into Analytica from the opened workbook.
+
To read a spreadsheet in Analytica, the user must first open a workbook using [[SpreadsheetOpen]](). Then the user can use [[SpreadsheetRange]]() to determine what to import into Analytica from the opened workbook.
  
In the example model, the variable nodes '''Spreadsheet Workbook''' and '''Spreadsheet Range''' contain SpreadsheetOpen() and SpreadsheetRange(), respectively. The definition of Spreadsheet Workbook is:
+
In the example model, the variable nodes <code>Spreadsheet Workbook</code> and <code>Spreadsheet Range</code> contain [[SpreadsheetOpen]]() and [[SpreadsheetRange]](), respectively. The definition of <code>Spreadsheet Workbook</code> is:
  
SpreadsheetOpen('example.xlsx',True)
+
:<code>SpreadsheetOpen('example.xlsx', True)</code>
  
And the definition of Spreadsheet Range is:
+
The definition of [[SpreadsheetRange]] is:
Definition of Spreadsheet Range''': SpreadsheetRange(Spreadsheet_Workbook,'Raw Data!',howToIndex:12)
+
:<code>SpreadsheetRange(Spreadsheet_Workbook, 'Raw Data!', howToIndex: 12)</code>
  
'''Spreadsheet Open''' opens a workbook file. It will look for the file named "example.xlsx", and even if it finds that file in the same directory as the model, it will prompt the user to select a file. The user can select a file with any name. For more details on different parameter options, see [[Functions To Read Excel Worksheets#SpreadsheetOpen(filename, showDialog) | SpreadsheetOpen()]].
+
<code>Spreadsheet Open]</code> opens a workbook file. It looks for a file named <code>example.xlsx</code> in the same directory as the model, but even if such a file exists, evaluating this node prompts the user to select a file. The user can select a file with any name. For more details on different parameter options, see [[Functions To Read Excel Worksheets#SpreadsheetOpen(filename, showDialog) | SpreadsheetOpen()]].
  
'''Spreadsheet Range''' will look for the sheet named "Raw Data" in the workbook opened in Spreadsheet Open and import all of the data contained in that sheet. It will create implicit indexes named Row and Column out of the first row and first column of the data, and use those to index the data. For more details on different parameter options, see [[Functions To Read Excel Worksheets#SpreadsheetRange(workbook, range, colIndex, rowIndex, howToIndex, sheet, what) | SpreadsheetRange()]].
+
<code>Spreadsheet Range</code>looks for the sheet named <code>Raw Data</code> in the workbook opened in<code>Spreadsheet Open]</code> and imports all of the data contained in that sheet. It creates implicit indexes named <code>Row</code> and <code>Column</code> out of the first row and first column of the data, and uses those to index the data. For more details on different parameter options, see [[Functions To Read Excel Worksheets#SpreadsheetRange(workbook, range, colIndex, rowIndex, howToIndex, sheet, what) | SpreadsheetRange()]].
  
 
===Opening Multiple Spreadsheets per Session===
 
===Opening Multiple Spreadsheets per Session===
Analytica will only prompt the user to select a spreadsheet until a spreadsheet has been selected that session. If the user would like to open more than one spreadsheet for the same variable in the same session, the link between Analytica and the opened spreadsheet must be cut. This can be done by changing the definition of '''Spreadsheet Open'''.  
+
The example model prompts the user to select a spreadsheet every time the '''Read Spreadsheet''' button is pressed, regardless of whether a spreadsheet has already been selected in that session.
  
In the example model, this is done in the OnClick of the Read Spreadsheet button as follows:
+
Generally speaking, when a node contains [[SpreadsheetOpen]](), even if the «showDialog» parameter is set to <code>True</code> it only prompts the user to select a spreadsheet until a spreadsheet has been selected. Once a spreadsheet has been selected, Analytica will not ask again. If the user would like to open more than one spreadsheet for the same variable in the same session, the link between Analytica and the opened spreadsheet must be cut. This can be done by changing the definition of <code>Spreadsheet Open</code>.
  
DEFINITION OF Spreadsheet_Workbook := 0;
+
In the example model, this is accomplished in the [[OnClick]] of the '''Read Spreadsheet''' button as follows:
DEFINITION OF Spreadsheet_Workbook := "SpreadsheetOpen('example.xlsx',True)";
 
  
Both lines must be used in order to reset the definition -- if only the second line of code was used, because it is exactly the same as the existing definition Analytica would not reset the workbook link and would continue to use the existing workbook.
+
:<code>Spreadsheet_Workbook := 0;</code>
 +
:<code>DEFINITION OF Spreadsheet_Workbook := "SpreadsheetOpen('example.xlsx', True)";</code>
 +
 
 +
Both lines must be used in order to reset the definition -- if only the second line of code was used, because it is exactly the same as the existing definition Analytica will not reset the workbook link and will continue to use the existing workbook. The first line cuts the workbook link and the second line resets the definition to a call to [[SpreadsheetOpen]]().
  
 
===Saving Spreadsheet Data to a New Variable===
 
===Saving Spreadsheet Data to a New Variable===
It is best practice to copy data from a spreadsheet into a new variable so that the data can be manipulated and the model can run even if the spreadsheet is not available. In the example model, this is done in the OnClick code of the '''Read Spreadsheet''' button as follows.
+
It is best practice to copy data from a spreadsheet into a new variable so that the data can be manipulated and the model can run even if the spreadsheet is not available. In the example model, this is done in the [[OnClick]] code of the '''Read Spreadsheet''' button as follows.
  
Car_Type := CopyIndex(Spreadsheet_Range.Row);
+
:<code>Car_Type := CopyIndex(Spreadsheet_Range.Row);</code>
Car_Info := CopyIndex(Spreadsheet_Range.Column);
+
:<code>Car_Info := CopyIndex(Spreadsheet_Range.Column);</code>
 +
:<code>Saved_Data := Spreadsheet_Range[@.Row = @Car_Type, @.Column = @Car_Info];</code>
  
Saved_Data := Spreadsheet_Range[@.Row=@Car_Type,@.Column=@Car_Info];
+
The first two lines copy the implicit indexes <code>Row</code> and </code>Column</code> into existing Index nodes <code>Car Type</code> and <code>Car Info</code>, respectively. The last line copies the data from </code>Spreadsheet Range</code> into the <code>Saved Data</code> variable, and reindexes the data from the implicit indexes to the new global indexes.
 
 
The first two lines copy the implixit indexes '''Row''' and '''Column''' into existing Index nodes '''Car Type''' and '''Car Info''', respectively. The last line copies the data from '''Spreadsheet Range''' into the '''Saved Data''' variable, and reindexes the data from the implicit indexes to the new global indexes.
 
  
 
==Writing to Spreadsheets==
 
==Writing to Spreadsheets==
Once a workbook has been opened in Analytica, the user can write data to the workbook using SpreadsheetSetCell(), SpreadsheetSetRange(), and SpreadsheetSetInfo().  
+
Once a workbook has been opened in Analytica, the user can write data to the workbook using [[SpreadsheetSetCell]](), [[SpreadsheetSetRange]](), and [[SpreadsheetSetInfo]]().  
  
<tip title="Note"> If any changes are made to a workbook from Analytica, SpreadsheetSave() must be used for those changes to be saved</tip>
+
<tip title="Note"> If any changes are made to a workbook from Analytica, [[SpreadsheetSave]]() must be called for those changes to be saved</tip>
  
 
In the example model, this is done in the OnClick of the '''Save Results to Spreadsheet''' button:
 
In the example model, this is done in the OnClick of the '''Save Results to Spreadsheet''' button:
  
SpreadsheetSetCell( Spreadsheet_Workbook, "Miles per Year", 1, @Car_Type, Car_Type);
+
:<code>SpreadsheetSetCell(Spreadsheet_Workbook, "Gallons per Year", 1, @Car_Type, Car_Type);</code>
SpreadsheetSetCell( Spreadsheet_Workbook, "Miles per Year", 2, @Car_Type, Miles_per_Year );
+
:<code>SpreadsheetSetCell(Spreadsheet_Workbook, "Gallons per Year", 2, @Car_Type, Gallons_per_Year);</code>
 
+
:<code>SpreadsheetSave(Spreadsheet_Workbook,'saved_example');</code>
SpreadsheetSave(Spreadsheet_Workbook,'saved_example');
 
  
The first two lines write the contents of the '''Car Type''' index to the first column in the sheet named "Miles per Year" and the variable '''Miles per Year''' to the second column in the sheet named "Miles per Year". For more details on different parameter options, see [[Functions to Write Data to Excel Worksheets#SpreadsheetSetCell( workbook, sheet, col, row, value ) | SpreadsheetSetCell()]].
+
The first two lines write the contents of the <code>Car Type</code> index to the first column in the sheet named <code>Gallons per Year</code> and the variable <code>Gallons per Year</code> to the second column in the sheet named <code>Gallons per Year</code>. For more details on different parameter options, see [[Functions to Write Data to Excel Worksheets#SpreadsheetSetCell(workbook, sheet, col, row, value) | SpreadsheetSetCell()]].
  
The last line saves the updated spreadsheet into a new file named 'saved_example'. This file is automatically saved in the same directory as the opened workbook. For more details on different parameter options, see [[Functions to Write Data to Excel Worksheets#SpreadsheetSave( workbook, filename ) | SpreadsheetSave()]].
+
The last line saves the updated spreadsheet into a new file named <code>saved_example</code>. This file is automatically saved in the same directory as the opened workbook. For more details on different parameter options, see [[Functions to Write Data to Excel Worksheets#SpreadsheetSave( workbook, filename ) | SpreadsheetSave()]].
  
If no workbook is already open and the '''Save Results to Spreadsheet''' button, Analytica will first prompt the user to select a workbook before completing the calculations. However, Analytica will only execute the OnClick of the '''Save Results to Spreadsheet''' button if a workbook is opened this way and ignore any OnClick code in the '''Read Spreadsheet''' button (i.e., it will not copy the data from the opened spreadsheet to Saved Data and therefore will be saving the existing results to the spreadsheet, not results from the newly uploaded spreadsheet).
+
If no workbook is already open and the '''Save Results to Spreadsheet''' button, Analytica first prompts the user to select a workbook before completing the calculations. However, it will only execute the [[OnClick]] of the '''Save Results to Spreadsheet''' button if a workbook is opened this way and ignore any OnClick code in the '''Read Spreadsheet''' button (i.e., it will not copy the data from the opened spreadsheet to Saved Data and therefore will be saving the existing results to the spreadsheet, not results from the newly uploaded spreadsheet).
  
 
==See Also==
 
==See Also==
*[[Spreadsheets in ACP]]
+
* [[Media:SpreadsheetExampleDTA.zip| Example model and spreadsheet]]
*[[Functions To Read Excel Worksheets]]
+
* [[Media:Functions for Reading Excel Worksheets.ana|Reading Excel Worksheets.ana]]
*[[Functions to Write Data to Excel Worksheets]]
+
* [[Excel spreadsheets read and write]]
 +
* [[Functions To Read Excel Worksheets]]
 +
* [[Functions to Write Data to Excel Worksheets]]
 +
<!--* [[Spreadsheets in ACP]]-->
 +
* [[Working with Excel Worksheets using the Excel 2013 Object Model.]]

Latest revision as of 23:22, 9 February 2023



This page goes over the following example model on how to read and write to spreadsheets in desktop Analytica.

SpreadsheetExamplePicDTA.jpg

In the example model, the Read Spreadsheet button prompts a user to select a workbook and copies the contents of that workbook into the Saved Data node. The Save Results to Spreadsheet button copies the results of the Miles per Year node into the original workbook file, and saves that as a new workbook file named saved_example.

Reading Spreadsheets

To read a spreadsheet in Analytica, the user must first open a workbook using SpreadsheetOpen(). Then the user can use SpreadsheetRange() to determine what to import into Analytica from the opened workbook.

In the example model, the variable nodes Spreadsheet Workbook and Spreadsheet Range contain SpreadsheetOpen() and SpreadsheetRange(), respectively. The definition of Spreadsheet Workbook is:

SpreadsheetOpen('example.xlsx', True)

The definition of SpreadsheetRange is:

SpreadsheetRange(Spreadsheet_Workbook, 'Raw Data!', howToIndex: 12)

Spreadsheet Open] opens a workbook file. It looks for a file named example.xlsx in the same directory as the model, but even if such a file exists, evaluating this node prompts the user to select a file. The user can select a file with any name. For more details on different parameter options, see SpreadsheetOpen().

Spreadsheet Rangelooks for the sheet named Raw Data in the workbook opened inSpreadsheet Open] and imports all of the data contained in that sheet. It creates implicit indexes named Row and Column out of the first row and first column of the data, and uses those to index the data. For more details on different parameter options, see SpreadsheetRange().

Opening Multiple Spreadsheets per Session

The example model prompts the user to select a spreadsheet every time the Read Spreadsheet button is pressed, regardless of whether a spreadsheet has already been selected in that session.

Generally speaking, when a node contains SpreadsheetOpen(), even if the «showDialog» parameter is set to True it only prompts the user to select a spreadsheet until a spreadsheet has been selected. Once a spreadsheet has been selected, Analytica will not ask again. If the user would like to open more than one spreadsheet for the same variable in the same session, the link between Analytica and the opened spreadsheet must be cut. This can be done by changing the definition of Spreadsheet Open.

In the example model, this is accomplished in the OnClick of the Read Spreadsheet button as follows:

Spreadsheet_Workbook := 0;
DEFINITION OF Spreadsheet_Workbook := "SpreadsheetOpen('example.xlsx', True)";

Both lines must be used in order to reset the definition -- if only the second line of code was used, because it is exactly the same as the existing definition Analytica will not reset the workbook link and will continue to use the existing workbook. The first line cuts the workbook link and the second line resets the definition to a call to SpreadsheetOpen().

Saving Spreadsheet Data to a New Variable

It is best practice to copy data from a spreadsheet into a new variable so that the data can be manipulated and the model can run even if the spreadsheet is not available. In the example model, this is done in the OnClick code of the Read Spreadsheet button as follows.

Car_Type := CopyIndex(Spreadsheet_Range.Row);
Car_Info := CopyIndex(Spreadsheet_Range.Column);
Saved_Data := Spreadsheet_Range[@.Row = @Car_Type, @.Column = @Car_Info];

The first two lines copy the implicit indexes Row and Column into existing Index nodes Car Type and Car Info, respectively. The last line copies the data from Spreadsheet Range into the Saved Data variable, and reindexes the data from the implicit indexes to the new global indexes.

Writing to Spreadsheets

Once a workbook has been opened in Analytica, the user can write data to the workbook using SpreadsheetSetCell(), SpreadsheetSetRange(), and SpreadsheetSetInfo().

Note
If any changes are made to a workbook from Analytica, SpreadsheetSave() must be called for those changes to be saved

In the example model, this is done in the OnClick of the Save Results to Spreadsheet button:

SpreadsheetSetCell(Spreadsheet_Workbook, "Gallons per Year", 1, @Car_Type, Car_Type);
SpreadsheetSetCell(Spreadsheet_Workbook, "Gallons per Year", 2, @Car_Type, Gallons_per_Year);
SpreadsheetSave(Spreadsheet_Workbook,'saved_example');

The first two lines write the contents of the Car Type index to the first column in the sheet named Gallons per Year and the variable Gallons per Year to the second column in the sheet named Gallons per Year. For more details on different parameter options, see SpreadsheetSetCell().

The last line saves the updated spreadsheet into a new file named saved_example. This file is automatically saved in the same directory as the opened workbook. For more details on different parameter options, see SpreadsheetSave().

If no workbook is already open and the Save Results to Spreadsheet button, Analytica first prompts the user to select a workbook before completing the calculations. However, it will only execute the OnClick of the Save Results to Spreadsheet button if a workbook is opened this way and ignore any OnClick code in the Read Spreadsheet button (i.e., it will not copy the data from the opened spreadsheet to Saved Data and therefore will be saving the existing results to the spreadsheet, not results from the newly uploaded spreadsheet).

See Also

Comments


You are not allowed to post comments.