Google Sheets - automatic lines & formating

89 views
Skip to first unread message

Hugo Oberson

unread,
Apr 11, 2023, 6:23:29 PM4/11/23
to Google Apps Script Community

Good evening

 

I need to create a google sheets with possible inputs from 3 persons.

 

The sheet should contain the following columns :
1. Day

2. Date

3. Type

4. Participant/s

5. Topic

6. Comment

 

I would like to have an automatic addition of a row above the date, once entered.

 

I also would appreciate each odd row in a specific colour and even row in other.

 

Could you please help in this regard please ?

Nerio Villalobos

unread,
Apr 12, 2023, 8:37:10 AM4/12/23
to google-apps-sc...@googlegroups.com
Puedes seguir los siguientes pasos:

Crea una nueva hoja de cálculo de Google.

En la fila 1, agrega las siguientes encabezados de columna: "día", "fecha", "tipo", "participante/s", "tema" y "comentar".

En la celda A2, ingresa la fórmula "=Hoy()" para obtener automáticamente la fecha actual.

Selecciona las celdas A1 a F1, haz clic con el botón derecho del mouse y selecciona "Formatear celdas". Selecciona la pestaña "Fondo" y elige un color para las filas pares y otro color para las filas impares.

Para agregar una fila automáticamente cada vez que se ingresa una entrada, puedes usar una secuencia de comandos de Google Apps Script. Abre la hoja de cálculo y haz clic en "Herramientas" y luego en "Editor de secuencias de comandos". A continuación, pega el siguiente código:

function onEdit(event) {
  var sheet = event.source.getActiveSheet();
  if (sheet.getName() !== 'Hoja1') { // Reemplaza 'Hoja1' con el nombre de tu hoja
    return;
  }
  var editedCell = sheet.getActiveCell();
  var columnToCheck = 2; // Cambia esto al número de la columna de "fecha"
  if (editedCell.getColumn() == columnToCheck) {
    var newRowIndex = editedCell.getRow() + 1;
    sheet.insertRowAfter(newRowIndex);
    var newRowRange = sheet.getRange(newRowIndex + ":" + newRowIndex);
    sheet.getRange("A" + newRowIndex).setValue(new Date());
    newRowRange.setBorder(true, true, true, true, true, true);
  }
}

Este código agregará una fila después de la fila actual en la que se ingresa una entrada y agregará automáticamente la fecha en la columna "día". También agregará bordes a la nueva fila para que se destaque.

Guarda y nombra la secuencia de comandos, luego cierra la ventana del editor.

Ahora, cada vez que ingrese una entrada en la hoja de cálculo, se agregará automáticamente una fila debajo de la entrada y se agregará la fecha en la columna "día". Las filas impares y pares también se resaltarán con diferentes colores.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/2674937d-912b-49c7-8fee-4c500b9a98d3n%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina
Message has been deleted
Message has been deleted

Hugo Oberson

unread,
Apr 12, 2023, 4:01:07 PM4/12/23
to Google Apps Script Community
Hi Nerio
Thanks for your reply
Did you get mines ?

Nerio Villalobos

unread,
Apr 13, 2023, 7:36:43 AM4/13/23
to google-apps-sc...@googlegroups.com
I'm glad it worked for you, anything I'm at your service

Regards,

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

Nerio Villalobos

unread,
Apr 15, 2023, 4:22:52 PM4/15/23
to google-apps-sc...@googlegroups.com
To have the onEdit() function add a new row above the edited row, instead of below it, you can use the insertRowBefore() function instead of insertRowAfter() to insert the row at the desired position.

Here is the modified code with this change:


function onEdit(event) {
  var sheet = event.source.getActiveSheet();
  if (sheet.getName() !== 'Hoja1') { // Reemplaza 'Hoja1' con el nombre de tu hoja
    return;
  }
  var editedCell = sheet.getActiveCell();
  var columnToCheck = 2; // Cambia esto al número de la columna de "fecha"
  if (editedCell.getColumn() == columnToCheck) {
    var newRowIndex = editedCell.getRow();
    sheet.insertRowBefore(newRowIndex);

    var newRowRange = sheet.getRange(newRowIndex + ":" + newRowIndex);
    sheet.getRange("A" + newRowIndex).setValue(new Date());
    newRowRange.setBorder(true, true, true, true, true, true);
  }
}

In this code, the line sheet.insertRowBefore(newRowIndex); replace the line sheet.insertRowAfter(newRowIndex); from the original code to insert the row above the edited row. With this change, the new row will be added above the edited row.


El sáb, 15 abr 2023 a la(s) 14:57, Hugo Oberson (hugo.o...@gmail.com) escribió:

Hi Nerio

Many thanks for your help

Unfortunately, it is adding a row UNDER and not ABOVE

And it is not keeping the format nor the formulas

 




I managed with this :

 

function onEdit(e) {  var sheet = e.range.getSheet();  var column = e.range.getColumn();  var row = e.range.getRow();   if (column === 5 && e.value) { // If a value is entered in column 5 (Date)    sheet.insertRowBefore(row); // Insert a new row before the edited cell    var lastRow = sheet.getLastRow();    var range = sheet.getRange(row - 1, 1, 1, sheet.getLastColumn() - 1); // Include an extra column on the right    var targetRange = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn() + 1); // Include an extra column on the right    var formulas = range.getFormulas(); // Get the formulas from the previous row    targetRange.setFormulas(formulas); // Set the formulas in the new row  }}

 

Adding a row BEFORE, but it is not copying the formulas

Reply all
Reply to author
Forward
0 new messages