Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
Exception: The number of rows in the range must be at least 1

I'm trying to run this code that will get the first 7 columns from sheet1 in spreadsheet1 and paste them in sheet2 in spreadsheet2, but im keep getting this error which I dont know why.

Exception: The number of rows in the range must be at least 1. 6th line

function MoveCode(){
  const ss1 = SpreadsheetApp.getActive();
  const ss2 = SpreadsheetApp.openById("1zU__ccPIMst54whmyrbmRnDRRjOtQBFPzXhw6NsFqpU");//or url whatever
  const ssh = ss1.getSheetByName('Sheet1');
  const dsh = ss2.getSheetByName('Sourcing');
  const vs = ssh.getRange(2,1,ssh.getLastRow() - 1,7).getValues();
  dsh.getRange(2,1,vs.length,vs[0].length).setValues(vs);
}

test3 is the spreadsheet that i want to paste the data to from.

If you need anymore explanation please let me know

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think that in your script when ssh.getLastRow() is less than 2, such an error occurs. Because ssh.getLastRow() - 1 is 0 and -1. For example, I think that when the sheet "Sheet1" has only the header row and no values, such an error occurs. In order to remove this issue, how about the following modification?

Modified script:

function MoveCode(){
  const ss1 = SpreadsheetApp.getActive();
  const ss2 = SpreadsheetApp.openById("###");//or url whatever
  const ssh = ss1.getSheetByName('Sheet1');
  const dsh = ss2.getSheetByName('Sourcing');
  const lastRow = ssh.getLastRow();
  if (lastRow < 2) return;
  const vs = ssh.getRange(2, 1, lastRow - 1, 7).getValues();
  dsh.getRange(2, 1, vs.length, vs[0].length).setValues(vs);
}
  • By this modification, when the sheet "Sheet1" has only the header row and no values, the script is finished.

Added:

From your replying and adding the sample images, when I saw your images, I thought that in your script, ssh and dsh are used as the source sheet and destination sheet, respectively. But in your sample image, I thought that you might want to use ssh and dsh as the destination sheet and the source sheet, respectively. If my understanding is correct, how about the following sample script?

Modified script:

In this case, please be careful about SpreadsheetApp.getActive() and SpreadsheetApp.openById("###") for each sheet.

function MoveCode(){
  const ss1 = SpreadsheetApp.getActive();
  const ss2 = SpreadsheetApp.openById("###");//or url whatever
  const ssh = ss1.getSheetByName('Sheet1');
  const dsh = ss2.getSheetByName('Sourcing');
  const lastRow = dsh.getLastRow();
  if (lastRow < 2) return;
  const vs = dsh.getRange(2, 1, lastRow - 1, 7).getValues();
  ssh.getRange(2, 1, vs.length, vs[0].length).setValues(vs);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda