Home > Enterprise >  How to transform excel VBA code into Javascript for Google Sheets?
How to transform excel VBA code into Javascript for Google Sheets?

Time:04-10

everyone. I need to change a code that I created in VBA to javascript and put it into Google Sheets. Does anyone know how I can do this? The code is below

Sub Influencers_automacao()
  Sheets(1).Activate
  Range("A1").Select
  Selection.End(xlDown).Select
  k = Selection.Row
  For t = 2 To Sheets.Count
    j = 2
    For i = 2 To k
      If Sheets(1).Cells(i, 1) = Sheets(t).Name Then
        Sheets(t).Cells(j, 1) = Sheets(1).Cells(i, 2)
        Sheets(t).Cells(j, 2) = Sheets(1).Cells(i, 3)
        j = j   1
      End If
    Next
  Next
End Sub

CodePudding user response:

I'm afraid the answer is that you have to learn javascript and the google sheets object model, or at least enough of it to figure out how to do the same things in js that you're doing in VBA.

However, you can try using the macro converter, and your existing VBA is pretty simple so it might work without alterations, but you might have to tweak it. Let me know if this works, or if you encounter errors with the converted code and we can try to work through them.

https://developers.google.com/apps-script/guides/macro-converter/convert-files

CodePudding user response:

Try

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var output = []
  ss.getSheets().forEach(function (sh, i) {
    if (i > 0) output.push([
      sh.getName(), sh.getRange('A1').getValue(), sh.getRange('B1').getValue()
    ])
  })
  ss.getSheets()[0].getRange(2, 1, output.length, output[0].length).setValues(output)
}

change A1, B1 as necessary

  • Related