Create search box in google docs

0

Good,

I have this VBA code and I needed to build a Javascript for Google Docs that does the same thing. Is it possible to convert VBA to Javascript?

Thank you.

Private Sub CommandButton1_Click()
Dim F1 As Worksheet
Dim intervalo As Range
Dim LastRow As Long
'Declara a Planilha pelo nome
Set F1 = ThisWorkbook.Worksheets("Folha1")
'Última Linha
LastRow = F1.Cells(F1.Rows.Count, "A").End(xlUp).Row

Set intervalo = F1.Range("A2:A" & LastRow)
'Caso não encontre TextBox2
If TextBox2 = "" Then
    MsgBox "Insira o nº da ficha a consultar."
'Se os campos de TextBox estiverem preenchidos
ElseIf TextBox2 > "" And TextBox1 > "" Then
'Realiza a procura
    With intervalo
        Set cellFound = .Find(TextBox1, LookIn:=xlValues)
        If Not cellFound Is Nothing Then
            FirstAddress = cellFound.Address
            Do
                'Realiza Ação após encontrar, faz offset de uma coluna para direita e segue o hyperlink. Então após encontrar na Coluna A, segue o hyperlink da coluna B.
                cellFound.Offset(0, 1).Hyperlinks(1).Follow
                Set cellFound = .FindNext(cellFound)
            Loop While Not cellFound Is Nothing And cellFound.Address <> FirstAddress
        End If
    End With
End If
TextBox2.SetFocus

End Sub

Private Sub TextBox1_Change()
    Dim intervalo As Range
    Dim texto As String
    Dim codigo As Integer
    Dim pesquisa
    Dim mensagem
    Dim F1 As Worksheet
    Set F1 = ThisWorkbook.Worksheets("Folha1")
    LastRow = F1.Cells(F1.Rows.Count, "A").End(xlUp).Row
   On Error Resume Next
    codigo = TextBox1.Text

    Set intervalo = F1.Range("A2:B" & LastRow)
    pesquisa = Application.WorksheetFunction.VLookup(codigo, intervalo, 2, False)

    TextBox2.Text = pesquisa


End Sub

'Private Sub UserForm_Terminate()
'ActiveWorkbook.Close Savechanges:=False
'End Sub
    
asked by anonymous 09.02.2018 / 12:45

1 answer

1

I made a solution in App Script that would be a conversion of your VBA. I hope that's what you need.

The working file link is this: link

Two files have been created in App Script:

.gp

function onOpen(e) {
   DocumentApp.getUi()
       .createMenu('Pesquisar')
       .addItem('Documentos', 'showSidebar')
       .addToUi();
}

function onInstall(e) {
  onOpen(e);
}

function showSidebar() {
  var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
      .setTitle('Pesquisar Documentos');
  DocumentApp.getUi().showSidebar(ui);
}

function BuscarCodigo(codigo) {

  if(codigo != null) {
    DocumentApp.getUi().alert(codigo);
  }

  var table = DocumentApp.getActiveDocument().getBody().getTables();

  var codigos = [];
  var links = [];
  var x = 1;
  while(x < table[0].getNumRows()) {
    codigos.push(table[0].getCell(x, 0).getText());
    links.push(table[0].getCell(x, 1).getText());
    x++;
  }

  var y = 0;
  var link = "";
  while(y < codigos.length) {
    if(codigo == codigos[y]) {
      DocumentApp.getUi().alert(links[y]);
      link = links[y];
      break;
    }
    y++;
  }

  AbrirLink(link);

}

function AbrirLink(link) {
  var ui = HtmlService.createHtmlOutput(
  "<!DOCTYPE html> " +
  "<html> " +
  "<head> " +    
    "<script type='text/javascript'> " +    
    "function AbrirLink(){ " +
    " var popup = window.open('http://" + link + "'); " +
    " google.script.run.showSidebar(); " +     
    "} " +  
    "</script> " +  
  "</head> " +
  "<body onload='AbrirLink()'> " +
  "</body> " +  
  "</html> "  
  );
  DocumentApp.getUi().showSidebar(ui);
}

Sidebar.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  </head>
  <body onload="document.formulario.codigo.focus();">

    <style>
    .hidden {
      display: none;
    }
    </style>

    <br />

    <form role="form" id="formulario" name="formulario" align="center">

      <div class="form-group col-xs-12">
        <label class="sr-only" for="codigo">Código</label>
        <input type="text" class="form-control" name="codigo" id="codigo" placeholder="Insira o código do Documento">
      </div>

      <button class="btn btn-success hidden" align="center" id="pesquisaroculto">
        Pesquisar
      </button>

      <button class="btn btn-success" align="center" id="pesquisar">
        Pesquisar
      </button>

    </form>

    <script src="https://code.jquery.com/jquery.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
  </body>

  <script type="text/javascript">

    $(function() {
        $('#pesquisar').click(runPesquisa);
        $('#pesquisaroculto').click(runPesquisaOculto);
    });

    function runPesquisaOculto() {

      $("#pesquisaroculto").prop("disabled",true);
      $( "#pesquisar" ).removeClass("hidden");
      $("#pesquisar").prop("disabled",false);
      $( this ).addClass("hidden");

      var codigo = $('input[name=codigo]').val();

      google.script.run.BuscarCodigo(codigo);

    }

    function runPesquisa() {

      $("#pesquisar").prop("disabled",true);
      $( "#pesquisaroculto" ).removeClass("hidden");
      $("#pesquisaroculto").prop("disabled",false);
      $( this ).addClass("hidden");

      var codigo = $('input[name=codigo]').val();

      google.script.run.BuscarCodigo(codigo);

    }

  </script>

</html>

It is only the logic without blank field validation that can be done in html normally. All new values entered in the table will work.

You can not do a search field within Google Docs.

What I did was a Document Search menu in the menu called "Search & Documents" which opens a sidebar with a field to enter the document code and a button to search the document.

Clicking this button links the searched document to a new tab.

    
16.03.2018 / 18:58