我目前將檔案保存在一個共享點上,該共享點記錄了我們所做的所有測驗。測驗完成后,我想將測驗(整行)移動到單獨的作業表中,以基本上存檔測驗計劃(它還需要將資料粘貼到下一個空行中),并且因為它現在已存檔從提交表中洗掉它。兩個作業表都在同一個作業簿中,并且每次運行腳本時行數都會不同,因此腳本代碼需要根據用戶的選擇輸入(在運行腳本之前或由腳本提示)作業)。
我已經設法使用以下代碼在 VBA 中執行此操作,但我似乎無法找到選擇功能的可行替代方案。有什么方法可以將下面的 VBA 代碼基本上翻譯成 Office 腳本,所以在 excel 在線中也會發生同樣的事情?
`Sub MoveCompletedTests()
Selection.Copy Sheets("Archive - ATL staff only").Range("A1048576").End(xlUp).Offset(1, 0)
Selection.Delete
End Sub`
作為參考,我在“樣本提交”表上有一個按鈕,該按鈕在該表上的選定范圍上運行上述代碼,將其移動到“存檔 - 僅限 ATL 員工”表。
我嘗試使用腳本記錄器,但腳本不允許選擇足夠動態,它為單元格范圍編碼,而不僅僅是選擇。
我非常感謝提供的任何幫助。
親切的問候,
本
uj5u.com熱心網友回復:
我認為類似的東西getSelectedRange應該可以作業(至少,如果你沒有同時使用幾個選定的范圍):
function main(workbook: ExcelScript.Workbook)
{
let destination: ExcelScript.Range;
let source = workbook.getSelectedRange().getEntireRow();
let archive = workbook.getWorksheet("Archive - ATL staff only");
if(archive.getUsedRange() == undefined) {
// if the archive sheet is empty, use the first row as a destination
destination = archive.getRange("1:1");
}
else {
// ... otherwise, use the next row after the last used
destination = archive.getUsedRange().getRowsBelow().getEntireRow();
}
destination.copyFrom(source, ExcelScript.RangeCopyType.values);
source.delete(ExcelScript.DeleteShiftDirection.up);
}
ps 需要明確的是,在 VBA 的情況下,Selection 是Application 的屬性。但在 ExcelScript 的情況下,它是 Workbook object 的一種方法。這可能有點令人困惑,因為有一個ExcelScript.Application介面,其中沒有提及選擇。
uj5u.com熱心網友回復:
您可以在這里嘗試代碼:
function main(workbook: ExcelScript.Workbook) {
//Assign the source variable to the selected range
let source: ExcelScript.Range = workbook.getSelectedRange()
//Assign the archive variable to the Archive worksheet
let archive: ExcelScript.Worksheet = workbook.getWorksheet("Archive - ATL staff only");
//Set the destination range in the archive sheet
let destination: ExcelScript.Range = archive.getRange("A:A").getExtendedRange(ExcelScript.KeyboardDirection.up).getLastCell().getOffsetRange(1,0)
//Determine if the offset range is set to A2 in the Archive sheet.
//If so, determine if A1 has a value. If it does not, update the
//destination range to cell A1.
if (destination.getAddress() === "'Archive - ATL staff only'!A2" ){
if (destination.getOffsetRange(-1,0).getValue() === "") {
destination = destination.getOffsetRange(-1,0)
}
}
//Copy the selected source range to the destination range
destination.copyFrom(source, ExcelScript.RangeCopyType.all);
//Delete the data in the source range
source.delete(ExcelScript.DeleteShiftDirection.up);
}
此代碼根據存檔表中 A 列中的資料確定最后一個單元格。如果存檔表的使用范圍在某處有資料,但 A 列沒有,這可能很有用。在這種情況下,此代碼仍會正確更新。
此外,此代碼不會復制和洗掉整行。因此,如果您打算使用小于整行的選擇,則此代碼可能會更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/467236.html
上一篇:區分Catia中的物體
下一篇:VBA中的IIF函式
