Sep 12 2024 09:20 AM
Total newb here...
This should be very simple.
Automate > Record Actions
Right-Click to the left of Column A (This highlights the entire sheet)
Sort > Custom Sort
Sort on Col J ("Amount" is the heading)
The result, when done manually, is just fine.
When recorded as a script and run it only sorts Column J, not the entire sheet like when it is done manually.
This is to be a 3 step process.
Sep 12 2024 12:06 PM - edited Sep 12 2024 12:11 PM
SolutionI'm not sure how recorder works, the script could be
function main(workbook: ExcelScript.Workbook) {
const range = workbook
.getActiveWorksheet()
.getUsedRange()
const sortedColumnIndex = range
.getRow(0) // get first row, i.e. headers
.find("Amount", { completeMatch: true })
.getColumnIndex()
range.getSort().apply( // apply to entire range
[{
ascending: true, // sort order
key: sortedColumnIndex // on column to sort
}],
false, //case insensitive
true, // range with headers
ExcelScript
.SortOrientation
.rows // sort by rows
)
}
We select only range with data, not entire sheet. Find column number for the cell with "Amount". Apply sorting to entire range, not to one column. Use above column as one on which we do sorting.
Sep 12 2024 12:38 PM
As for #3 didn't catch what exactly you are trying to do. Currently sort orientation is columns. That means values in second row of the range (key=1, numbering starts from zero) are taken, and columns are placed in order according to values in that row.
Only one sorting key works.
If you'd like to sort the range by rows that's as in previous case.
Sep 12 2024 12:55 PM
As for the # 1 you may simplify it a bit
function main(workbook: ExcelScript.Workbook) {
const range = workbook
.getActiveWorksheet()
.getUsedRange()
const format: ExcelScript.RangeFormat = range.getFormat()
format.setHorizontalAlignment(ExcelScript.HorizontalAlignment.general)
format.setVerticalAlignment(ExcelScript.VerticalAlignment.bottom)
format.setWrapText(false)
format.setTextOrientation(0)
format.setIndentLevel(0)
}
Sep 12 2024 01:00 PM
Sep 12 2024 01:03 PM
Sep 12 2024 01:44 PM - edited Sep 12 2024 01:46 PM
Multicolumn sort could be
function main(workbook: ExcelScript.Workbook) {
const range = workbook
.getActiveWorksheet()
.getUsedRange()
const columnIndex = (header: string): number =>
range.getRow(0)
.find(header, { completeMatch: true })
.getColumnIndex()
range.getSort().apply(
[
{ key: columnIndex("Customer Name"), ascending: true }
, { key: columnIndex("Description"), ascending: true }
, { key: columnIndex("Date"), ascending: true }
],
false,
true,
ExcelScript
.SortOrientation
.rows)
}
Sep 12 2024 01:57 PM
Non-printable characters also could be removed by the script, but for that desirably to know does your text includes only English alphabet or not; if not which one. If possible non-printable characters are known - even better.
Sep 13 2024 05:53 AM
Sep 13 2024 12:30 PM
@mikemilstead , you are welcome
Sep 12 2024 12:06 PM - edited Sep 12 2024 12:11 PM
SolutionI'm not sure how recorder works, the script could be
function main(workbook: ExcelScript.Workbook) {
const range = workbook
.getActiveWorksheet()
.getUsedRange()
const sortedColumnIndex = range
.getRow(0) // get first row, i.e. headers
.find("Amount", { completeMatch: true })
.getColumnIndex()
range.getSort().apply( // apply to entire range
[{
ascending: true, // sort order
key: sortedColumnIndex // on column to sort
}],
false, //case insensitive
true, // range with headers
ExcelScript
.SortOrientation
.rows // sort by rows
)
}
We select only range with data, not entire sheet. Find column number for the cell with "Amount". Apply sorting to entire range, not to one column. Use above column as one on which we do sorting.