Nerea
Nerea

Reputation: 319

delete the contents of a row if it finds a match Google Apps Script

What I want to do:

I have a sheet called "Añadir ficha". On that sheet, in cell K6 there is a value.

I have another sheet called "BD Fichas" with all the data.

If the value of cell K6 of sheet "Añadir ficha" matches the value of any cell in column B of sheet "BD Fichas", then it clears the content and data validations (the format cell NO clear) of that row.

This is my code. For some reason I can't get it to work.

function eliminar_registro(){

  var libro = SpreadsheetApp.getActiveSpreadsheet();
  var hojaOrigen = libro.getSheetByName('Añadir ficha');
  var hojaDestino = libro.getSheetByName('BD Fichas');

  var valorBuscado = hojaOrigen.getRange('K6').getValue(); 
  var tablaDeBusqueda = hojaDestino.getRange('B8:B').getValues(); 

  var listaDeBusqueda = tablaDeBusqueda.map(function(row){return row[0]}); 

  var busqueda = listaDeBusqueda.indexOf(valorBuscado); 

  tablaDeBusqueda[busqueda][0]; 

  var fila = busqueda.getRow();

  if (valorBuscado == tablaDeBusqueda[busqueda][0]) {
   fila.clearContent();
  }
}

I am new to JavaScript and Google Apps Script. All help is well appreciated.

Thank you very much

Upvotes: 0

Views: 94

Answers (1)

JPV
JPV

Reputation: 27242

See if this helps

function eliminar_registro() {

  const libro = SpreadsheetApp.getActiveSpreadsheet();
  const matchValue = libro.getSheetByName('Añadir ficha').getRange('K6').getValue()
  const hojaDestino = libro.getSheetByName('BD Fichas')
  hojaDestino.getRange('B8:B').getValues().flat().forEach((v, i) => {
      if (v == matchValue) hojaDestino.getRange(i + 8, 1, 1, hojaDestino.getLastColumn()).clear({contentsOnly: true, validationsOnly: true})
    })
}

Upvotes: 2

Related Questions