Al seleccionar una celda de un rango el valor de la celda se copie a una definida

30 views
Skip to first unread message

Oscar Bernilla

unread,
Sep 7, 2021, 8:54:11 PM9/7/21
to Google Apps Script Community
Estuve intentando realizar la programación pero me muestra el mismo error:

Lo que se quiere lograr es lo siguiente:

Ejercicio.jpg

Le comparto lo programado:

function LeerDatos(){
var sheets = SpreadsheetApp; // conexión con googleSheets
var archivo = sheets.getActiveSpreadsheet(); // conexión con la hoja de calculo - getActiveSpreadsheet()  =archivo activo / actual
var hoja = archivo.getActiveSheet(); //accediendo a la pestaña
var celdaActiva = hoja.getActiveCell();
var filaActiva = celdaActiva.getRow();
var colActiva = celdaActiva.getColumn();
//var valor =celdaActiva.getA1Notation();
var valor = celdaActiva.setValue();

/*
Logger.log(filaActiva);
Logger.log(colActiva);
Logger.log(valor);
*/

if(filaActiva > 3 && colActiva == 2 )
{
  //archivo.getSheetByName('Prueba').getRange('C4').setValue(celdaActiva);
  archivo.getSheetByName('Prueba').copyTo(valor.getRange('C4'))
}

He revisado en otros métodos y usan la función : onEdit(e) lo intente pero tuve el siguiente resultado:

function onEdit(e) {
var filaActual = e.range.getRow();
var colActual = e.range.getColumn();
  if ((filaActual !=3) || (colActual != 2)) {return;}
  
    var archivo = SpreadsheetApp.getActiveSpreadsheet();
    var hoja = archivo.getActiveSheet();
    var nombreHoja = hoja.getSheetName();

    if (nombreHoja != "Prueba") {return;}
    var oldValue = e.oldValue;
    var valueToCheck = sheet.getRange("B4:B30").getValue();

   if (valueToCheck > 5 && valueToCheck != oldValue)
   {
   hoja.getRange('C4').copyTo(hoja.getRange('C4'))
   }
}

Este es el error que siempre sale, he programado de una manera distinta con onEdit(e), pero no he encontrado información el uso de '(e)'
onEdit_error.jpg
Espéro me puedan ayudar, gracias

Peter C

unread,
Sep 8, 2021, 5:31:44 AM9/8/21
to Google Apps Script Community
Hello,
The OnEdit () function allows you to trigger an action if a modification has been made in the file.
The "e" is not useful to you here in my opinion.
To carry out your operation, you must integrate the variables into your function or as common variables.
"var sheets = SpreadsheetApp; // connection to googleSheets
var file = sheets.getActiveSpreadsheet (); // connection to the spreadsheet - getActiveSpreadsheet () = active / current file
var sheet = file.getActiveSheet (); // access to the tab"

Example to point to the current record by the user and get the line => var ActiveRow = sheet.getActiveCell (). GetRow ()

Hope to have been useful to you.
Excellent day

Oscar Bernilla

unread,
Sep 8, 2021, 1:20:36 PM9/8/21
to Google Apps Script Community
Lo intente de la siguiente manera pero me sale el mismo mensaje:  TypeError: hojaActual.getRange is not a function

function leerDato() {
  var hojaActual = SpreadsheetApp.getActiveSpreadsheet().getName();
  var celdaActiva = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell();
  var filaActiva = celdaActiva.getRow();
  var colActiva = celdaActiva.getColumn();
  var valor = celdaActiva.getValue();
//Logger.log(valor);

if(filaActiva > 3 && colActiva === 2 && SpreadsheetApp.getActiveSheet().getName() === 'Prueba'){
  hojaActual.getRange(valor).getValue.copyTo(hojaActual.getRange('C4'))
  
  //Logger.log(valor);
}
}

cbmserv...@gmail.com

unread,
Sep 8, 2021, 4:00:47 PM9/8/21
to google-apps-sc...@googlegroups.com

Oscar,

 

Your definition of hojaActual is wrong. You need to point to the sheet (not its name) for you to be able to use getRange method on it.

 

As in this:

 

function leerDato() {

  var hojaActual = SpreadsheetApp.getActiveSheet();

Espéro me puedan ayudar, gracias

--
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/ef4c3cfa-06c5-47a8-bc51-255c0a38e5a4n%40googlegroups.com.

Oscar Bernilla

unread,
Sep 8, 2021, 4:39:38 PM9/8/21
to Google Apps Script Community
Muchas gracias @George,

Me alegraste el día...

Reply all
Reply to author
Forward
0 new messages