Skip to content

Commit ceadde5

Browse files
committed
reformat
1 parent f9b5f41 commit ceadde5

17 files changed

Lines changed: 108 additions & 126 deletions

scripts/tables/add-rows-to-table.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ function main(
1212
) {
1313
const table = workbook.getTable(tableName);
1414
if (!table) {
15-
throw new Error(`Table "${tableName}" not found.`);
15+
throw new Error(`Table '${tableName}' not found.`);
1616
}
1717

1818
const columnNames = table.getColumns().map(col => col.getName().trim());
19+
1920
let inputData: Record<string, string>[];
2021
try {
2122
inputData = JSON.parse(inputJson);
@@ -31,13 +32,17 @@ function main(
3132
const inputKeys = Object.keys(inputData[0]).map(k => k.trim());
3233
for (const key of inputKeys) {
3334
if (!columnNames.includes(key)) {
34-
throw new Error(`Input key "${key}" does not match any column in the table "${tableName}".`);
35+
throw new Error(`Input key '${key}' does not match any column in the table '${tableName}'.`);
3536
}
3637
}
3738

3839
// build 2D array matching table column order
39-
const rows = inputData.map(obj => columnNames.map(colName => obj[colName] ?? undefined));
40+
const rows = inputData.map(obj =>
41+
columnNames.map(colName => obj[colName] ?? undefined)
42+
);
4043
console.log(rows);
44+
4145
table.addRows(-1, rows);
46+
4247
return "Rows added successfully.";
4348
}

scripts/tables/auto-fit-column-widths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function main(
99
) {
1010
const table = workbook.getTable(tableName);
1111
if (!table) {
12-
throw new Error(`Table "${tableName}" not found.`);
12+
throw new Error(`Table '${tableName}' not found.`);
1313
}
1414

1515
table.getRange().getFormat().autofitColumns();

scripts/tables/clear-cell-contents.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ function main(
1313
) {
1414
const table = workbook.getTable(tableName);
1515
if (!table) {
16-
throw new Error(`Table "${tableName}" not found.`);
16+
throw new Error(`Table '${tableName}' not found.`);
1717
}
1818

1919
// Get all headers and find the column index
2020
const headers = table.getHeaderRowRange().getValues()[0];
2121
const columnIndex = headers.indexOf(columnName);
22+
2223
if (columnIndex === -1) {
23-
throw new Error(`Column "${columnName}" not found.`);
24+
throw new Error(`Column '${columnName}' not found.`);
2425
}
2526

26-
// Get the row (excluding the header row);
27+
// Get the row (excluding the header row)
2728
const rowRange = table.getRangeBetweenHeaderAndTotal();
2829
const targetCell = rowRange.getCell(rowIndex, columnIndex);
30+
2931
targetCell.clear(ExcelScript.ClearApplyTo.contents);
3032
}

scripts/tables/convert-name-cases.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function main(
1919
columnsToFix.forEach(columnName => {
2020
const column = table.getColumnByName(columnName);
2121
if (!column) {
22-
throw new Error(`Input key "${columnName}" does not match any column in the table "${tableName}".`);
22+
throw new Error(`Input key '${columnName}' does not match any column in the table '${tableName}'.`);
2323
}
2424

2525
let values = column.getRange().getValues();
@@ -35,12 +35,12 @@ function main(
3535
if (cellValue === cellValue.toUpperCase() || cellValue === cellValue.toLowerCase()) {
3636
// Convert to Proper Case while handling accents
3737
const properCaseValue = cellValue.toLowerCase().replace(
38-
/(^|\\s)([a-záéíóúüñâàäêëîïôöûüç])/g,
38+
/(^|\s)([a-záéíóúüñâàäêëîïôöûüç])/g,
3939
(_, boundary, letter) => boundary + letter.toUpperCase()
4040
);
4141

4242
// Update the value in the array
43-
values[i][0] = properCaseValue
43+
values[i][0] = properCaseValue;
4444
}
4545
}
4646

scripts/tables/convert-table-column-from-formula-to-values.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,30 @@ function main(
1414
fullColumn: boolean = false,
1515
numberOfRowsFromEnd?: number
1616
) {
17-
if (!fullColumn && !numberOfRowsFromEnd) {
18-
throw new Error(`Parameter "numberOfRowsFromEnd" is required when fullColumn is set to false.`);
19-
}
20-
2117
const table = workbook.getTable(tableName);
2218
if (!table) {
23-
throw new Error(`Table "${tableName}" not found.`);
19+
throw new Error(`Table '${tableName}' not found.`);
2420
}
2521

2622
const column = table.getColumnByName(columnName);
2723
if (!column) {
28-
throw new Error(`Column "${columnName}" not found.`);
24+
throw new Error(`Column '${columnName}' not found.`);
2925
}
3026

3127
// clear filters so following actions are not impacted
3228
table.getAutoFilter().clearCriteria();
3329

3430
const totalColRange = column.getRangeBetweenHeaderAndTotal();
31+
3532
let range: ExcelScript.Range;
3633
if (fullColumn) {
3734
range = totalColRange;
35+
3836
} else if (numberOfRowsFromEnd) {
3937
range = totalColRange.getLastCell().getOffsetRange(1 - numberOfRowsFromEnd, 0).getAbsoluteResizedRange(numberOfRowsFromEnd, 1);
38+
4039
} else {
41-
throw new Error(`numberOfRowsFromEnd required when fullColumn is false.`);
40+
throw new Error(`Parameter 'numberOfRowsFromEnd' is required when fullColumn is set to false.`);
4241
}
4342

4443
range.copyFrom(

scripts/tables/create-pivot-table.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
2-
* Creates a pivot table from an existing table with specified row and value aggregations.
3-
*
4-
* @param tableName Name of the source table for the pivot table.
5-
* @param location Where to place the pivot table: "New sheet" creates a new worksheet, "Existing sheet" places it below the source table unless a sheet name is specified.
6-
* @param rowsColumn Column name to use for pivot table rows.
7-
* @param valuesColumns Array of column names to aggregate in the pivot table values area.
8-
* @param valuesOperation Aggregation function to apply to the values columns.
9-
* @param sheetName The name of the sheet the pivot table should be placed on when location is Existing sheet (defaults to same sheet as table). If location is New sheet, this is the name the new sheet should have.
10-
* @param pivotTableName Optional name for the pivot table (auto-generates if blank or already exists);
11-
*/
2+
* Creates a pivot table from an existing table with specified row and value aggregations.
3+
*
4+
* @param tableName Name of the source table for the pivot table.
5+
* @param location Where to place the pivot table: "New sheet" creates a new worksheet, "Existing sheet" places it below the source table unless a sheet name is specified.
6+
* @param rowsColumn Column name to use for pivot table rows.
7+
* @param valuesColumns Array of column names to aggregate in the pivot table values area.
8+
* @param valuesOperation Aggregation function to apply to the values columns.
9+
* @param sheetName The name of the sheet the pivot table should be placed on when location is Existing sheet (defaults to same sheet as table). If location is New sheet, this is the name the new sheet should have.
10+
* @param pivotTableName Optional name for the pivot table (auto-generates if blank or already exists)
11+
*/
1212
function main(
1313
workbook: ExcelScript.Workbook,
1414
tableName: string,
@@ -22,22 +22,22 @@ function main(
2222
) {
2323
const table = workbook.getTable(tableName);
2424
if (!table) {
25-
throw new Error(`Table "${tableName}" not found.`);
25+
throw new Error(`Table '${tableName}' not found.`);
2626
}
2727

2828
if (table.getRowCount() === 0) {
29-
throw new Error(`Table "${tableName}" has no data.`);
29+
throw new Error(`Table '${tableName}' has no data.`);
3030
}
3131

3232
// make sure specified columns exist
3333
const tableCols = table.getColumns().map((col) => col.getName());
34-
let missingColumn: string;
3534
if (!tableCols.includes(rowsColumn)) {
36-
throw new Error(`There is no column "${rowsColumn}" in table "${tableName}".`);
37-
} else {
35+
throw new Error(`There is no column '${rowsColumn}' in table '${tableName}'.`);
36+
}
37+
else {
3838
valuesColumns.forEach((colName) => {
3939
if (!tableCols.includes(colName)) {
40-
throw new Error(`There is no column "${colName}" in table "${tableName}".`);
40+
throw new Error(`There is no column '${colName}' in table '${tableName}'.`);
4141
}
4242
});
4343
}
@@ -89,7 +89,6 @@ function main(
8989
pivotTableName = defaultName + i;
9090
}
9191

92-
// create pivot table
9392
const pivotTable = workbook.addPivotTable(pivotTableName, table, locationRange);
9493

9594
// add rows field to pivot table

scripts/tables/create-table-from-json.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/**
2-
* Creates a table from JSON data on a specified worksheet.
3-
*
4-
* @param sheetName Name of the worksheet where the table will be created
5-
* @param startCell Top-left cell address for the table (e.g., "A1")
6-
* @param inputJson JSON string containing array of objects with consistent keys
7-
* @param tableName Optional name for the created table
8-
*/
2+
* Creates a table from JSON data on a specified worksheet.
3+
*
4+
* @param sheetName Name of the worksheet where the table will be created
5+
* @param startCell Top-left cell address for the table (e.g., "A1")
6+
* @param inputJson JSON string containing array of objects with consistent keys
7+
* @param tableName Optional name for the created table
8+
*/
99
function main(
1010
workbook: ExcelScript.Workbook,
1111
sheetName: string,
1212
startCell: string,
1313
inputJson: string,
1414
tableName?: string
1515
) {
16-
let returnMsg: string
16+
let returnMsg: string;
1717

1818
const sheet = workbook.getWorksheet(sheetName);
19-
if (!sheet) throw new Error(`Worksheet "${sheetName}" not found.`);
19+
if (!sheet) throw new Error(`Worksheet '${sheetName}' not found.`);
2020

2121
let inputData: Record<string, string | number | boolean>[];
2222
try {
@@ -36,17 +36,18 @@ function main(
3636
// convert startCell to a full range
3737
const startRange = sheet.getRange(startCell);
3838
const endRange = startRange.getOffsetRange(0, numCols - 1);
39-
const tableRange = `${startCell}:${endRange.getAddress().split('!')[1]}`
39+
const tableRange = `${startCell}:${endRange.getAddress().split('!')[1]}`;
4040

4141
// create table with headers only
4242
const table = sheet.addTable(tableRange, true);
43+
4344
if (tableName) {
4445
try {
4546
table.setName(tableName);
4647
} catch (e) {
4748
const allTables = workbook.getTables().map(tbl => tbl.getName());
4849
if (allTables.includes(tableName)) {
49-
returnMsg = `A table already exists with the name "${tableName}".`
50+
returnMsg = `A table already exists with the name '${tableName}'.`;
5051
} else {
5152
throw new Error(e);
5253
}
@@ -63,7 +64,9 @@ function main(
6364
// add data rows
6465
const rowsData = inputData.map(obj => inputKeys.map(key => obj[key]));
6566
table.addRows(-1, rowsData);
67+
6668
return {
67-
"message": "Successfully created table." + ((returnMsg) ? "\ " + returnMsg : ""), "createdTableName": table.getName()
68-
};
69+
"message": "Successfully created table." + ((returnMsg) ? "\n" + returnMsg : ""),
70+
"createdTableName": table.getName()
71+
}
6972
}

scripts/tables/highlight-specific-table-columns.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ function main(
2121
switch (matchType) {
2222
case "List of column names": {
2323
if (!columnNamesArray) {
24-
throw new Error(`Parameter columnNamesArray is required when matchType is "List of column names".`);
24+
throw new Error("Parameter columnNamesArray is required when matchType is 'List of column names'.");
2525
}
2626
break;
2727
}
2828
case "RegEx": {
2929
if (!regexPattern) {
30-
throw new Error(`Parameter regexPattern is required when matchType is "RegEx".`);
30+
throw new Error("Parameter regexPattern is required when matchType is 'RegEx'.");
3131
}
3232
break;
3333
}
3434
default: {
35-
throw new Error(`Parameter matchType has an unrecognised value. Valid values are "List of column names" and "RegEx".`);
35+
throw new Error("Parameter matchType has an unrecognised value. Valid values are 'List of column names' and 'RegEx'.");
3636
}
3737
}
3838

@@ -48,7 +48,7 @@ function main(
4848

4949
const table = workbook.getTable(tableName);
5050
if (!table) {
51-
throw new Error(`Table "${tableName}" not found.`);
51+
throw new Error(`Table '${tableName}' not found.`);
5252
}
5353

5454
const tableCols = table.getColumns();
@@ -78,14 +78,16 @@ function main(
7878
if (matchType === "List of column names") {
7979
const highlightedColNames = colsToHighlight.map((col) => col.getName());
8080
const missingCols = columnNamesArray.filter((name) => {
81-
return !highlightedColNames.includes(name);
81+
return !highlightedColNames.includes(name)
8282
});
8383

8484
return {
85-
"message": "Successfully highlighted matched columns.", "notFoundColumns": missingCols || []
86-
};
85+
"message": "Successfully highlighted matched columns.",
86+
"notFoundColumns": missingCols || []
87+
}
8788
}
89+
8890
return {
8991
"message": "Successfully highlighted matched columns."
90-
};
92+
}
9193
}

scripts/tables/set-table-rows-height.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
2-
* Sets the row height for all rows in a table.
3-
*
4-
* @param tableName Name of the table to modify
5-
* @param rowHeight Height in points to apply to all table rows (defaults to 14.25)
6-
*/
2+
* Sets the row height for all rows in a table.
3+
*
4+
* @param tableName Name of the table to modify
5+
* @param rowHeight Height in points to apply to all table rows (defaults to 14.25)
6+
*/
77
function main(
88
workbook: ExcelScript.Workbook,
99
tableName: string,
1010
rowHeight?: number
1111
) {
1212
const table = workbook.getTable(tableName);
1313
if (!table) {
14-
throw new Error(`Table "${tableName}" not found.`);
14+
throw new Error(`Table '${tableName}' not found.`);
1515
}
1616

1717
// use default row height unless provided

scripts/tables/sort-table-by-column-name.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ function main(
1111
) {
1212
const table = workbook.getTable(tableName);
1313
if (!table) {
14-
throw new Error(`Table "${tableName}" not found.`);
14+
throw new Error(`Table '${tableName}' not found.`);
1515
}
1616

1717
// Get the column index based on the column name
1818
const columnIndex = table.getHeaderRowRange().getValues()[0].indexOf(columnName);
19-
if (columnIndex !== -1) {
20-
table.getSort().apply([{ key: columnIndex, ascending: true }]);
21-
} else {
22-
throw new Error(`Column "${columnName}" not found.`);
19+
20+
if (columnIndex === -1) {
21+
throw new Error(`Column '${columnName}' not found.`);
2322
}
23+
24+
table.getSort().apply([{ key: columnIndex, ascending: true }]);
2425
}

0 commit comments

Comments
 (0)