0

我正在尝试将宏 VBA 转换为 Office 脚本(Typescript)以在 Excel Web 中运行

我有这段 VBA 代码:

Sub process()
        On Error GoTo Error_Handler
    ' concatenate Macro

    Dim var As String
    Dim row As Integer
    Dim column As Integer
    Dim concatened As String
    Dim hours() As String
    Dim cursor As Integer
    Dim person As Integer
    Dim sum As Double
    
    ' zeroing columns

    For column = Sheets("Parameters").Cells(2, 4) To Sheets("Parameters").Cells(2, 5)
        For row = Sheets("Parameters").Cells(2, 2) To Sheets("Parameters").Cells(2, 3)
            
            Sheets("Projects").Cells(row, column) = ""
            
        Next row
        
        Sheets("Projects").Cells(3, column) = ""
        
    Next column

上下文是:我有一个名为 Projects 的主工作表,然后此循环扫描由名为 Parameters 的工作表确定的列和行并擦除其中的数据

细胞 2,4; 2,5; 2,2; 2,3 只需按列和行告诉他们从哪里开始和在哪里停止

起初我是这样做的:

    //catch active worksheet
  let selectedWorksheet = workbook.getActiveWorksheet();
  

  // select range to scan
  let range = selectedWorksheet.getRange("EK3:HL3");

  // zeroing columns
  range.setValue("");

但我想像 VBA 代码一样自动化“EK3:HL3”,通过参数工作表进行编辑,而不是在代码中手动更改,以防 EK3:HL3 范围变大....

如果你们能照亮我的心灵,我真的很高兴

4

1 回答 1

1

您可以尝试使用getRangeByIndexes.

    const wsParameters = Workbook.getWorksheet("Parameters")
    const wsProjects = Workbook.getWorksheet("Projects")
    
    const startRow = wsParameters.getRange("B2").getValue()
    const startCol = wsParameters.getRange("D2").getValue()
    const noRow = wsParameters.getRange("C2").getValue() - startRow + 1
    const noCols = wsParameters.getRange("E2").getValue() - startCol + 1

    let rng = wsProjects.getRangeByIndexes(startRow, startCol, noRow, noCols)

    rng.setValue("")
于 2021-05-27T00:54:15.650 回答