Convert excel VBA Macro to OfficeScript
hi,
Can you help me to convert this vba to a typescript?
Sub kolommenverbergen()
Dim col As Long, fr As Long, lr As Long
Dim rVis As Range, cell As Range
Dim bHide As Boolean
Application.ScreenUpdating = False
With ActiveSheet.ListObjects("table")
fr = .Range.Row
lr = fr + .Range.Rows.Count - 1
.Range.EntireColumn.Hidden = False
If .ListColumns(1).Range.SpecialCells(xlVisible).Cells.Count < lr - fr + 1 Then
For col = 2 To .Range.Columns.Count
Set rVis = .ListColumns(col).DataBodyRange.SpecialCells(xlVisible)
bHide = True
For Each cell In rVis
If Len(cell.Value) Then
bHide = False
Exit For
End If
Next cell
.ListColumns(col).Range.EntireColumn.Hidden = bHide
Next col
End If
End With
Application.ScreenUpdating = True
End Sub
this is what I have but doesn't working
function updateColumnsVisibility() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const tableRange = sheet.getDataRange();
const table = sheet.getRange(tableRange.getRow(), tableRange.getColumn(), tableRange.getLastRow(), tableRange.getLastColumn());
const numRows = table.getNumRows();
const numCols = table.getNumColumns();
sheet.setColumnWidths(1, numCols, 100); // Reset column widths
// Loop through each column in the table
for (let col = 1; col <= numCols; col++) {
let visibleCount = 0;
let bHide = true;
// Loop through each row in the column's range
for (let row = 1; row <= numRows; row++) {
const cell = table.getCell(row, col);
if (cell.getValue() !== '') {
visibleCount++;
}
}
// If there are any non-empty cells in the column, don't hide it
if (visibleCount > 0) {
bHide = false;
}
// Hide or show the entire column based on the visibility flag
sheet.setColumnHidden(col, bHide);
}
}