Home > database >  Setvalues() from one spreadsheet to another not working
Setvalues() from one spreadsheet to another not working

Time:06-01

I'm trying to copy various cells from one spreadsheet and pasting them to another spreadseet. I have debugged the script multiple times and there are no bugs, but it is still not setting the values in the sheet.

function Pegarfechas(){

  var ficha = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FICHA');
  var hojafechas = SpreadsheetApp.openById("1rCFTJuBoWgPgJ_9PV13VIApGYn8pSFa4S6VYAx5tOmU").getSheetByName('Fechas');
  var codigo=ficha.getRange("H2").getValue();
  var color =ficha.getRange("R2").getValue();
  var Sku = codigo   color;
  var fechas = ficha.getRange("D24:P24").getValues();
 // I defined the variables for better understanding      
  var startRow = hojafechas.getLastRow() 1; 
  var startColumn = 1
  hojafechas.getRange(startRow, startColumn).setValues[Sku, fechas];
};

I think the problem is at "hojafechas.getRange(startRow, startColumn).setValues[Sku,fechas];" but I have also tried to set the values separately and it still did not work. Does anybody know what could be happening and how I could fix it? I want to paste Sku in Column 1 in the other spreadsheet and "fechas" in the Columns right next to it. Thank you so much in advance :)

How i want it to look :)

CodePudding user response:

Try:

function Pegarfechas(){

  const ficha = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FICHA');
  const hojafechas = SpreadsheetApp.openById("1rCFTJuBoWgPgJ_9PV13VIApGYn8pSFa4S6VYAx5tOmU").getSheetByName('Fechas');
  const codigo = ficha.getRange("H2").getValue();
  const color = ficha.getRange("R2").getValue();
  const Sku = codigo   color;
  const fechas = ficha.getRange("D24:P24").getValues().flat();

  const values = [[Sku, ...fechas]];

  hojafechas.getRange(1, 1, 1, values[0].length).setValues(values);

};

Explained:

// Creates a 'row' of data, beginning with `Sku` and `fechas`'s values spread out.
const values = [[Sku, ...fechas]];

// Using the dimensions of the `values` array, set the values in the sheet.
hojafechas.getRange(1, 1, 1, values[0].length).setValues(values);
  • Related