1

tengo este código funcionando (copio solo una parte)

var valores = [[FACTURA.getRange(CELDA_NRO_FACTURA).getValue(),  //Nro Factura
                 FACTURA.getRange(CELDA_FECHA).getValue(),   //Fecha
                 FACTURA.getRange(CELDA_MEDIO_PAGO1).getValue(),   //Medio de pago
                 FACTURA.getRange(CELDA_DTO_RGO_GENERAL).getValue(),   //Dto/Rgo general
            
            //Fila 6
             FACTURA.getRange(PRIMERA_FILA,PRIMERA_COLUMNA).getValue(),  //Código
             FACTURA.getRange(PRIMERA_FILA,PRIMERA_COLUMNA + 1).getValue(),  //Cantidad
             FACTURA.getRange(PRIMERA_FILA,PRIMERA_COLUMNA + 2).getValue(),  //Descripción
             FACTURA.getRange(PRIMERA_FILA,PRIMERA_COLUMNA + 7).getValue(),  //Unitario
             FACTURA.getRange(PRIMERA_FILA,PRIMERA_COLUMNA + 8).getValue(),  //Dto/Rgo x fila

            //Fila 7
             FACTURA.getRange(PRIMERA_FILA + 1,PRIMERA_COLUMNA).getValue(),  //Código
             FACTURA.getRange(PRIMERA_FILA + 1,PRIMERA_COLUMNA + 1).getValue(),  //Cantidad
             FACTURA.getRange(PRIMERA_FILA + 1,PRIMERA_COLUMNA + 2).getValue(),  //Descripción
             FACTURA.getRange(PRIMERA_FILA + 1,PRIMERA_COLUMNA + 7).getValue(),  //Unitario
             FACTURA.getRange(PRIMERA_FILA + 1,PRIMERA_COLUMNA + 8).getValue(),  //Dto/Rgo x fila


            //Fila 8
             FACTURA.getRange(PRIMERA_FILA + 2,PRIMERA_COLUMNA).getValue(),  //Código
             FACTURA.getRange(PRIMERA_FILA + 2,PRIMERA_COLUMNA + 1).getValue(),  //Cantidad
             FACTURA.getRange(PRIMERA_FILA + 2,PRIMERA_COLUMNA + 2).getValue(),  //Descripción
             FACTURA.getRange(PRIMERA_FILA + 2,PRIMERA_COLUMNA + 7).getValue(),  //Unitario
             FACTURA.getRange(PRIMERA_FILA + 2,PRIMERA_COLUMNA + 8).getValue(),  //Dto/Rgo x fila

            //Fila 9
             FACTURA.getRange(PRIMERA_FILA + 3,PRIMERA_COLUMNA).getValue(),  //Código
             FACTURA.getRange(PRIMERA_FILA + 3,PRIMERA_COLUMNA + 1).getValue(),  //Cantidad
             FACTURA.getRange(PRIMERA_FILA + 3,PRIMERA_COLUMNA + 2).getValue(),  //Descripción
             FACTURA.getRange(PRIMERA_FILA + 3,PRIMERA_COLUMNA + 7).getValue(),  //Unitario
             FACTURA.getRange(PRIMERA_FILA + 3,PRIMERA_COLUMNA + 8).getValue(),  //Dto/Rgo x 
             ]];

datos.getRange(datos.getLastRow()+1,1,1,valores.length).setValues(valores);

y quisiera remplazarlo con este otro

var valores = [factura.getRange(CELDA_NRO_FACTURA).getValue(),  //Nro Factura
                 factura.getRange(CELDA_FECHA).getValue(),   //Fecha
                 factura.getRange(CELDA_MEDIO_PAGO1).getValue(),   //Medio de pago
                 factura.getRange(CELDA_DTO_RGO_GENERAL).getValue(),   //Dto/Rgo general
                 factura.getRange(CELDA_OBSERVACIONES).getValue()]  //Observaciones
  
  Logger.log(valores)

  const COLUMNA_CODIGO = 3
  const COLUMNA_CANTIDAD = 4
  const COLUMNA_DESCRIPCION = 5
  const COLUMNA_UNITARIO = 10
  const COLUMNA_DTO_RGO = 11
  const CANT_FILAS_FACTURA = 10
  var cont = 5
  
  for(fila=0;fila < CANT_FILAS_FACTURA; fila++){
    valores.push(factura.getRange(PRIMERA_FILA + fila,COLUMNA_CODIGO).getValue())  //Código
    valores.push(factura.getRange(PRIMERA_FILA + fila,COLUMNA_CANTIDAD).getValue())  //Cantidad
    valores.push(factura.getRange(PRIMERA_FILA + fila,COLUMNA_DESCRIPCION).getValue())  //Descripción
    valores.push(factura.getRange(PRIMERA_FILA + fila,COLUMNA_UNITARIO).getValue())  //Unitario
    valores.push(factura.getRange(PRIMERA_FILA + fila,COLUMNA_DTO_RGO).getValue())  //Dto/Rgo x fila
  }

  Logger.log(valores)
  datos.getRange(datos.getLastRow()+1,1,1,valores.length).setValues(valores); 

Mi problema es que soy principiante y no se como asignarle los valores a la variable "valores" en el for. Creo que se agregan con push, pero cuando lo ejecuto me da el siguiente error:

Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues.

1 respuesta 1

0

he revisado el código se debe a que falta corchetes.setValues([valores])

Alternativa:

const factura = SpreadsheetApp.getActive().getSheetByName('Sheet3');
const a1Notations = ['A1:A10', 'C1:C10', 'D1:D10']
const arrValores = factura.getRangeList(a1Notations).getRanges()
    .map(function(row){return row.getValues()});

const valores = transpose(arrValores)

var datos = SpreadsheetApp.getActive().getSheetByName('Sheet2');

datos.getRange(datos.getLastRow()+1,1,valores.length,valores[0].length).setValues(valores);}/* End if */

/**
 * Get traspose array
 * @see Ask question:
 * https://stackoverflow.com/questions/4492678/swap-rows-with-columns-transposition-of-a-matrix-in-javascript
 * @see Property code:
 * https://stackoverflow.com/users/4079309/kit-inwi
 */
function transpose(a) {
    return a[0].map(function (_, c) { return a.map(function (r) { return r[c]; }); });}

En esta otra alternativa utilizo método getRangeList() reduzco las llamadas a los valores con el método getValues() y retorna los valores a la variable matriz a una matriz con la función traspose() invierta las filas en columnas.
También he utilizado método getSheetByName() para obtener la clase Sheet.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.