Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

384
Views
App Script - ¿Cómo puedo acelerar mi función setBackground()?

Tengo problemas con mi función setBackground() en el script de la aplicación. ¿Cómo puedo acelerarlo? Está funcionando pero la ejecución es muy lenta.

He escrito esto:

 function changeColor(sheetName, startColorCol, sizeCellCol, totalCellCol) { var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName) for (z = startColorCol; z <= totalCellCol; z = z + sizeCellCol) { // As this is called onEdit() we don't want to perform the entire script every time a cell is // edited- only when a status cell is mofified. // To ensure this, before anything else we check to see if the modified cell is actually in the status column. if (sSheet.getActiveCell().getColumn() == z) { var row = sSheet.getActiveRange().getRow(); var value = sSheet.getActiveCell().getValue(); var col = "white"; // Default background color var colLimit = z; // Number of columns across to affect switch (value) { case "fait": col = "MediumSeaGreen"; break; case "sans réponse": col = "Orange"; break; case "proposition": col = "Skyblue"; break; case "Revisions Req": col = "Gold"; break; case "annulé": col = "LightCoral"; break; default: break; } if (row >= 3) { sSheet.getRange(row, z - 2, 1, sizeCellCol).setBackground(col); } } } }

Vi que podría necesitar usar operaciones por lotes, pero no tengo idea de cómo hacer que funcione.

La cuestión es que necesito colorear un rango de celdas cuando se cambia el valor de una. Algunas ideas ?

Gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Problemas:

  • Solo desea verificar una sola celda, la celda que se editó ( var value = sSheet.getActiveCell().getValue(); ). Por lo tanto, no tiene sentido usar un bucle.

Solución:

  • Use el objeto de evento para obtener los datos relacionados con la celda editada (hoja, índice de columna, índice de fila, etc.), en lugar de usar un bucle y verificar getActiveCell().getColumn() cada vez. Este objeto de evento se pasa a onEdit como un parámetro de forma predeterminada ( e en el ejemplo a continuación), pero debe pasarlo a su función changeColor como argumento.
  • Antes de hacer cualquier otra cosa, verifique si la celda editada es una de las celdas editadas que se encuentran en el rango que está rastreando (hoja correcta, fila sobre 3, columna entre startColorCol y totalCellCol .
  • Si la celda editada está en el rango adecuado, actualice los colores de fondo.

Ejemplo de código:

 function onEdit(e) { // ...Some stuff... changeColor(e, sheetName, startColorCol,sizeCellCol, totalCellCol); } function changeColor(e, sheetName, startColorCol,sizeCellCol, totalCellCol) { const range = e.range; const column = range.getColumn(); const row = range.getRow(); const sSheet = range.getSheet(); if (sSheet.getName() === sheetName && column >= startColorCol && column <= totalCellCol && row >= 3) { const value = range.getValue(); let col = "white"; // Default background color switch (value) { case "fait": col = "MediumSeaGreen"; break; case "sans réponse": col = "Orange"; break; case "proposition": col = "Skyblue"; break; case "Revisions Req": col = "Gold"; break; case "annulé": col = "LightCoral"; break; default: break; } sSheet.getRange(row, column-2, 1, sizeCellCol).setBackground(col); } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!