0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Excel Addin】Writeback data to the database

0
Posted at

Sharperlight basically extracts data from data sources (databases, Excel worksheets, API endpoints, etc.), but if you enable write-back settings, data can be written back to the data source . (this is called writeback in Sharperlight)
This time, I would like to use an Excel add-in to write the data on the sheet back to the database.

Database/Table Preparation

I created some kind of Ledger table in an existing test database. I would like to write data from an Excel worksheet to this table.
image.png

Preparing the Datamodel

First create a data model so that Sharperlight can interact with the data source.
I have already created a datamodel for this test database, so I would like to reflect only the information of the table added this time.
Launch Studio from the Sharperlight application menu.
Locate the target data model with the folder icon.
image.png
Select the Studio Connection sub-menu of the Tools menu and check if it is properly connected to the target database.
image.png
image.png
After checking, select the Import Tables menu from the Schema menu.
image.png
Clicking the Get Tables button will load the schema information from the connected database. Tables that have already been loaded into the data model are shown in red, new tables that have not yet been loaded are shown in black and ticked.
image.png
When you press the OK button, the ticked table information will be imported into the data model.
image.png
The newly imported table is displayed under the All Tables node.
image.png
In this state, the newly added table cannot be seen from the query builder etc. Move the new Ledger table to the Table Display node. You can move it by dragging and dropping.
If you select the Ledger table that has been moved to the Table Display node, the attribute list of the table will be displayed on the right side. Find the Allow Table Writeback attribute in it and set it to True.
image.png
You can now write data back to this table. This is simply a permission to write back to this table, and you can also set permission/non-permission on a user-by-user basis.

Now double-click on the Ledger table to display the field list in the center, and select that field to display its properties on the right.
Check the description is correct of those fields.
image.png
After all is done, save the data model with the OK button.

Data Preparation

Prepare such data on an Excel worksheet.
image.png
It's a good idea to match the column name with the field Description in the data model.

Create a Writeback Definition

Now let's define writeback. Select Writeback from the Sharperlight Excel add-in ribbon.
image.png
The writeback definition dialog is displayed. Select the datamodel just editted for Product and the ledger table for Table.
The field list of the ledger table is displayed in the lower Fields tab. Here we will map the data and fields on the Excel sheet.
image.png
With the input text field of the Code field selected, double-click the column header of the table on the Excel sheet. Then the automatic mapping function will work and you will see a message like this. Select Yes to proceed.
image.png
Mapping is set only if the column name of the data table and the description of the data model match.
This time they all match.
image.png
If there is a field that has no mapping set, with the input text field selected, double-click the header of the column you want to create a mapping on the Excel sheet. Then the same message as before will be displayed, so select the No button this time. That way you can map individual fields.
image.png
The Id field is an IDENTITY field, so the Id is automatically calculated by the database and no mapping is required.
There is a star to the left of the ID. This is because the Sharperlight engine uses the data in this column as a guide to check how many rows the table on the sheet has. However, this feature cannot be set for unmapped fields. So this time we set this function to the Code field.

Select the Code field and select Driver from the right-click menu.
image.png
Finally, enter Ledger for the writeback name. When you save the definition with the Close button, it will be saved with this name.
image.png
The definition is now complete.

Verification

Press the Preview button to check if the engine is picking up the data correctly according to the mapping settings.
image.png
A preview and log are displayed. If you look at the log, you can see that no writes have been made. This function is only for checking whether the data is correctly picked up.
image.png
The Validate button is a data collection check without preview. Only logs are displayed.
image.png
Since it was confirmed that the data was successfully picked up, execute the write back with the Execute button.
image.png
I could see the logs and it ran fine.
image.png
Let's check the database table. Writeback was done well. :thumbsup_tone1:
image.png

Updating and Inserting Data

Update the data on the Excel sheet.
Add a new record with code S012 at the bottom line. Also update the Payment amount and Balance of the record with code S006.
image.png
If you write back again in this state, new records will be added and changed records will be updated.
image.png
image.png

Creating Writeback Button

This time, instead of opening the writeback dialog and pressing the Execute button, we'll add a button to perform the writeback.
Select Buttons->Writeback->mdWriteBack on the Sharperlight Ribbon with the cell where you want to place the button selected.
image.png
A list of writeback definitions will be displayed. Select the definition Ledger created this time, and press the OK button.
image.png
The writeback button was created.
image.png
Add a new record at the bottom and press this button to perform a writeback.
image.png
image.png
Checked the database immediately and saw it was updated correctly.
image.png
This concludes the brief explanation of the Sharperlight Writeback function.

Thank you for reading this article.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?