Skip to content

Commit ea48a68

Browse files
committed
First doc and scripts
Added - general repository documentation - GatherSheetsSummary.bas and related documentation - ArrayForm2Database.ts and related documentation
1 parent fa7e563 commit ea48a68

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

ArrayForm2Database.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function main(workbook: ExcelScript.Workbook) {
2+
3+
// define a few names
4+
// worksheets
5+
const PARAM_SHEET_NAME = "Parametres"
6+
const SCHEDULE_SHEET_NAME = "Planning"
7+
const DATABASE_SHEET_NAME = "BaseDeDonnees"
8+
// tables
9+
const DATABASE_TABLE_NAME = "Database"
10+
// ranges
11+
const NUMBER_OF_COLUMNS_RANGE_NAME = "nb_col"
12+
const NUMBER_OF_ROWS_RANGE_NAME = "nb_row"
13+
const FIRST_COL_NUMBER_RANGE_NAME = "num_first_col"
14+
const FIRST_ROW_NUMBER_RANGE_NAME = "num_first_row"
15+
// internal script param
16+
const AFFAIR_NUM_COL_INDEX = 0 // zero based index in array of values from a range given by user
17+
const AFFAIR_NAME_COL_INDEX = 1
18+
const AFFAIR_DATE_ROW_INDEX = 0
19+
const AFFAIR_DAY_TIME_ROW_INDEX = 1
20+
21+
// init variables
22+
let param_sheet = workbook.getWorksheet(PARAM_SHEET_NAME)
23+
let schedule_sheet = workbook.getWorksheet(SCHEDULE_SHEET_NAME)
24+
let database_sheet = workbook.getWorksheet(DATABASE_SHEET_NAME)
25+
let database_table = workbook.getTable(DATABASE_TABLE_NAME)
26+
27+
// clear table (it is a full update)
28+
if (database_table.getRowCount() > 0) { // if database not already empty
29+
database_table.deleteRowsAt(0, database_table.getRowCount()) // delete all rows in database
30+
}
31+
32+
// get user param
33+
let nb_row = param_sheet.getRange(NUMBER_OF_ROWS_RANGE_NAME).getValue() as number
34+
let nb_col = param_sheet.getRange(NUMBER_OF_COLUMNS_RANGE_NAME).getValue() as number
35+
let num_first_col = param_sheet.getRange(FIRST_COL_NUMBER_RANGE_NAME).getValue() as number - 1 // convert from 1 indexed to 0 indexed
36+
let num_first_row = param_sheet.getRange(FIRST_ROW_NUMBER_RANGE_NAME).getValue() as number - 1
37+
38+
// get data (data structure is as follow : [Excel_row][Excel_col])
39+
let schedule_data = schedule_sheet.getRangeByIndexes(num_first_row, num_first_col, nb_row, nb_col).getValues()
40+
let dates_data = schedule_sheet.getRangeByIndexes(0,num_first_col,2,nb_col).getValues()
41+
let affairs_data = schedule_sheet.getRangeByIndexes(num_first_row, 0, nb_row, 2).getValues()
42+
43+
// loop through source range
44+
for (let row_index = 0; row_index < (nb_row - 1); row_index++)
45+
{
46+
for (let col_index = 0; col_index < (nb_col - 1); col_index++)
47+
{
48+
if (schedule_data[row_index][col_index] != "")
49+
{
50+
// save reorganised data to table
51+
database_table.addRow(
52+
0,
53+
[
54+
affairs_data[row_index][AFFAIR_NUM_COL_INDEX],
55+
affairs_data[row_index][AFFAIR_NAME_COL_INDEX],
56+
dates_data[AFFAIR_DATE_ROW_INDEX][col_index],
57+
dates_data[AFFAIR_DAY_TIME_ROW_INDEX][col_index],
58+
schedule_data[row_index][col_index]
59+
]
60+
)
61+
}
62+
}
63+
}
64+
}
65+

GatherSheetsSummary.bas

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Attribute VB_Name = "GatherSheetsSummary"
2+
Sub Main()
3+
4+
Dim sheet_index As Integer
5+
Dim index_offset As Integer ' sheet number to start with
6+
7+
' user param ' can become a user form
8+
index_offset = 5
9+
10+
Dim offset As Integer
11+
12+
offset_row = 0
13+
14+
Dim rng As Range
15+
16+
For sheet_index = index_offset To Application.Sheets.Count
17+
18+
Sheets(sheet_index).Select
19+
20+
Set rng = Range("A116:G128")
21+
22+
' Grab Some Data and Store it in a "Range" variable
23+
24+
Sheets("synthese_auto").Select
25+
26+
offset_row = 14 * (sheet_index - index_offset)
27+
' 14 is the number of rows contained in the range + 1 (to leave one row blank between tables)
28+
29+
' from : https://www.thespreadsheetguru.com/the-code-vault/best-way-to-copy-pastespecial-values-only-with-vba
30+
31+
' Transfer Values to same spot in another worksheet (Mimics PasteSpecial Values Only)
32+
Range("A1").offset(offset_row, 0).Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
33+
34+
Next
35+
36+
End Sub
37+
38+
39+

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
# excel-utility-scripts
2-
Common VBA and TypeScript scripts useful for daily Excel automation projects.
2+
A set of common VBA and TypeScript scripts useful for daily Excel automation projects.
3+
4+
## Keywords
5+
Excel ; VBA ; Typescript ; Macro ; Office Scripts ; Excel for the web ; Excel desktop ; Office Visual Basic for Applications
6+
7+
## Repository structure
8+
There are two types of scripts on this repository :
9+
10+
* [VBA scripts](https://learn.microsoft.com/en-us/office/vba/api/overview/) which are only compatible with desktop versions of Excel. These scripts are written in Visual Basic language and stored in ".bas" text files.
11+
* [Office scripts](https://learn.microsoft.com/en-us/office/dev/scripts/develop/scripting-fundamentals) which are only compatible with web versions of Excel and some desktop versions (require consistent internet connection and that file is located on OneDrive). These scripts are written in TypeScript language and stored in ".osts" text files.
12+
13+
## Installation
14+
* VBA scripts must be imported from the VBA IDE (from Excel desktop : alt + F11)
15+
* Office scripts on this GitHub repository can be copy-pasted in the Office Scripts IDE. Alternatively, you can deposit them on folder usally located in : your-OneDrive/Documents/Office scripts. Please note that, for development purposes, the Office scripts are stored in this repository in ".ts" format and would require to be restructured to comply to ".osts" format (not only renamed), so the copy-paste method explained above is recommended.
16+
17+
## Features
18+
The scripts available on this repository are listed and explained below.
19+
20+
### VBA - gather sheets summary
21+
*related file : GatherSheetsSummary.bas*
22+
For each sheet in a workbook, gather a range located always in the same cell on each sheet. This range contains e.g. a summary of data contained in the active sheet. So that the juxtaposition of ranges makes a summary of the whole workbook.
23+
24+
### Office - array form to database
25+
*related file : ArrayForm2Database.ts*
26+
Build and update a database based on an array form.
27+
More precisely, users fill the array form, and a script organize the data in a database (Excel table), which enables Pivot Tables or Power Platform usage of this data.

0 commit comments

Comments
 (0)