Home > Blockchain >  Compare texts in Google Apps Script
Compare texts in Google Apps Script

Time:06-17

Is there any way to compare texts in Apps script? I need to know if the value stored in the variable cellColumnaB contains the text "Jornada", but I don't know if there is a function that does it or if it has to be built.

function myFunction() {
    var objetoLibro = SpreadsheetApp.openById("1dv4FWDjhCJrruzDZsEyPPjUBHjUI-FJ9RhG68nL07qI");//abre el libro
    var objetoHoja = objetoLibro.getSheets()[0];//obtiene la hoja 1
    var dblUltimaFila = objetoHoja.getLastRow();//obtiene el numero de filas

    for (var i = 1; i < dblUltimaFila; i  ) {
        var cellColumnaB = objetoHoja.getRange(i, 2).getValues();//obtiene los valores de la columna 2
  
        if( cellColumnaB.includes("Jornada") ){
            Logger.log(cellColumnaB);
        }
    }
}

CodePudding user response:

Description

In your OP objetoHoja.getRange(i, 2).getValues(). getValues() returns a 2D array, not a single value. So include will not find the string "Jornada".

Might I suggest the following modifications to your script. Get all the values from objetoHoja and then loop through the 2D array to compare strings.

Code.gs

function myFunction() {
    var objetoLibro = SpreadsheetApp.openById("1dv4FWDjhCJrruzDZsEyPPjUBHjUI-FJ9RhG68nL07qI");//abre el libro
    var objetoHoja = objetoLibro.getSheets()[0];//obtiene la hoja 1
    var dblUltimaFila = objetoHoja.getDataRange().getValues();

    for (var i = 1; i < dblUltimaFila.length; i  ) {
        var cellColumnaB = dblUltimaFila[i][1];
  
        if( cellColumnaB.includes("Jornada") ){
            Logger.log(cellColumnaB);
        }
    }
}
  • Related