Forum Discussion
Why does Remove Duplicates Excel online Automated Script work when created but not when re-run?
- Apr 03, 2025
Without sample it's hard to say is sort orientation using correctly or not. Plus if you have responses in structured table it's easier and more reliable to manipulate with table rather than range.
IMHO, main point you shall to wait while data refresh will be done before continue with the next step. Below for that is sleep() function added. Set it on few seconds which will be enough to refresh with some gap. It depend on concrete data and query.
function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet() const range =sheet.getRange("A3:S1003") const all_responses = workbook.getWorksheet("all responses").getRange("A1:S1000") range.clear(ExcelScript.ClearApplyTo.contents); // Refresh all data connections //workbook.refreshAllDataConnections(); sleep(2) // Sleeps for 2 seconds range.getCell(0,0).copyFrom( all_responses, ExcelScript.RangeCopyType.all, false, false ) range.getColumn(10).getResizedRange(0, 4) .delete(ExcelScript.DeleteShiftDirection.left) const rangeUsed = range.getUsedRange() sheet.getAutoFilter().apply( rangeUsed.getRow(0) ) const rangeSort = rangeUsed .getRow(0) .getRowsBelow(rangeUsed.getRowCount() - 1) rangeSort.getSort() .apply( [ { key: 7, ascending: false } ], false, false, ExcelScript.SortOrientation.rows //.columns ) rangeUsed.removeDuplicates([2], true) rangeUsed.getSort() .apply( [{ key: 1, ascending: true }], false, true, ExcelScript.SortOrientation.rows) } // Delay in seconds function sleep(seconds: number) { const waitUntil = new Date().getTime() + seconds * 1000 while (new Date().getTime() < waitUntil) { } }
Without sample it's hard to say is sort orientation using correctly or not. Plus if you have responses in structured table it's easier and more reliable to manipulate with table rather than range.
IMHO, main point you shall to wait while data refresh will be done before continue with the next step. Below for that is sleep() function added. Set it on few seconds which will be enough to refresh with some gap. It depend on concrete data and query.
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getActiveWorksheet()
const range =sheet.getRange("A3:S1003")
const all_responses = workbook.getWorksheet("all responses").getRange("A1:S1000")
range.clear(ExcelScript.ClearApplyTo.contents);
// Refresh all data connections
//workbook.refreshAllDataConnections();
sleep(2) // Sleeps for 2 seconds
range.getCell(0,0).copyFrom(
all_responses,
ExcelScript.RangeCopyType.all, false, false
)
range.getColumn(10).getResizedRange(0, 4)
.delete(ExcelScript.DeleteShiftDirection.left)
const rangeUsed = range.getUsedRange()
sheet.getAutoFilter().apply( rangeUsed.getRow(0) )
const rangeSort = rangeUsed
.getRow(0)
.getRowsBelow(rangeUsed.getRowCount() - 1)
rangeSort.getSort()
.apply(
[
{ key: 7, ascending: false }
],
false, false, ExcelScript.SortOrientation.rows //.columns
)
rangeUsed.removeDuplicates([2], true)
rangeUsed.getSort()
.apply(
[{ key: 1, ascending: true }],
false, true, ExcelScript.SortOrientation.rows)
}
// Delay in seconds
function sleep(seconds: number) {
const waitUntil = new Date().getTime() + seconds * 1000
while (new Date().getTime() < waitUntil) { }
}