
私は、Power Automate で使用するために Excel Online の新しいスクリプト機能 (VBA ではありません) を使用しています。特定の時点以降の行を削除するスクリプトを作成しようとしています。最初はうまくいきましたが、その後エラーが発生し続けます。
以下の機能を使用しました:
最終的なスクリプトは次のとおりです。
function main(workbook: ExcelScript.Workbook) {
// [Directly from link 1]
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// [Directly from link 3]
// Get the first table on the current worksheet.
const currentSheet = workbook.getActiveWorksheet();
const table = currentSheet.getTables()[0];
// [Directly from link 1]
// Get the last used cell at the end of the column.
// This recreates the Ctrl+Down arrow key behavior.
let firstCell = selectedSheet.getRange("A1");
let firstColumn = selectedSheet.getRange("A1").getRangeEdge(ExcelScript.KeyboardDirection.down);
let cellAfter = firstColumn.getOffsetRange(1, 0);
// [My own creation using link 2]
// Saves the row number of the cell after the Ctrl+Down
let FinalRow = cellAfter.getRowIndex();
// [My own creation using link 4]
// Deletes all rows from the last row for 2000 rows.
// Without '-1' a blank row is left. '-1' deletes all the empty rows.
table.deleteRowsAt(FinalRow - 1,2000);
}
最初にスクリプトを実行したときは、問題は発生せず、正常に動作しました。2 回目にスクリプトを実行したときは正常に動作しましたが、「行 25: テーブル deleteRowsAt: 引数が無効であるか、欠落しているか、形式が正しくありません。」というエラーが発生しました。更新後、3 つ目の問題が発生し、スクリプトが「ExcelScript」でエラーを表示しますが、正常に動作し、「行 25...」エラーを表示します。下のスクリーンショットを参照してください。
「「ExcelScript」および「行 25...」エラーを示すスクリプト」
これをエラーなしで機能させる必要があります。Power Automate フローに追加すると、エラーによってフローが壊れてしまうからです。
Power Automate で「実行後」を設定して、「動作中の」スクリプトでエラーが発生しても続行できることはわかっています。ただし、Power Automate でスクリプトを実行すると「動作」しませんが、Excel Online で実行すると「動作」します。
ご協力に感謝いたします。