Forum Discussion
Excel Sort Script does not work
- Jul 18, 2021
getTable appears to be the appropriate method. However, they do not say if Table IDs starts at 0 or 1, I suspect 0 though
EDIT: Assuming the 2nd example on Combine data from multiple Excel tables into a single table works, the line of code:
const headerValues = workbook.getWorksheet(sheetNames[0]).getTables()[0]....
confirms Worksheets and Tables index start at 0
Could you try:
function main(workbook: ExcelScript.Workbook) { let selectedSheet = workbook.getActiveWorksheet(); // Get 1st Table on selectedSheet let firstTable = selectedSheet.getTable(0); firstTable.getSort().apply([{ key: 0, ascending: true }]); }And instead of by ID, by your Table name:
function main(workbook: ExcelScript.Workbook) { let selectedSheet = workbook.getActiveWorksheet(); // Get Table 'Clients' on selectedSheet let myTable = selectedSheet.getTable('Clients'); myTable.getSort().apply([{ key: 0, ascending: true }]); }If any option fails and you get an error message please provide that exact message
If the 1st (by ID = 0) works I would suggest you use that one so this will still work if you rename the Table later
EDIT There appears to be an Office Script space on stackoverflow
getTable appears to be the appropriate method. However, they do not say if Table IDs starts at 0 or 1, I suspect 0 though
EDIT: Assuming the 2nd example on Combine data from multiple Excel tables into a single table works, the line of code:
const headerValues = workbook.getWorksheet(sheetNames[0]).getTables()[0]....
confirms Worksheets and Tables index start at 0
Could you try:
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Get 1st Table on selectedSheet
let firstTable = selectedSheet.getTable(0);
firstTable.getSort().apply([{ key: 0, ascending: true }]);
}
And instead of by ID, by your Table name:
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Get Table 'Clients' on selectedSheet
let myTable = selectedSheet.getTable('Clients');
myTable.getSort().apply([{ key: 0, ascending: true }]);
}
If any option fails and you get an error message please provide that exact message
If the 1st (by ID = 0) works I would suggest you use that one so this will still work if you rename the Table later
EDIT There appears to be an Office Script space on stackoverflow
- J_Taylor40Jul 19, 2021Copper ContributorI tried the second one and it works.
Thank you so much, L z.- LorenzoJul 19, 2021Silver ContributorYou're welcome. Thanks for marking as response (can help others)
Nice day...