Home > Enterprise >  How Do I Get to Print A and B in Google App Script?
How Do I Get to Print A and B in Google App Script?

Time:12-13

I have a code someone from reddit gave me. Basically it's for separating all the data in A on different sheets (1k each)

Now I need to do the same but getting A and B seprated on different sheets (1k each sheet)

`

function splitInto1KSheets(){
  const sliceLength = 1000
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('new list');
  const values = sheet.getRange(1,1,2,sheet.getLastRow()).getValues()
  const rounds = values.length / sliceLength
  let n = 0
  

  for (let i = 0; i < rounds; i  ){
    const slice = values.slice(n, n   sliceLength)
    const newSheet = ss.insertSheet().setName(`${i 1}K`)
    newSheet.getRange(1,1,2,slice.length,1).setValues(slice)
    n  = sliceLength
  }

}

`

Errors:

"Exception: The number of columns in the data does not match the number of columns in the range. The data has 22880 but the range has 1."

and

"Exception: The parameters (number,number,number,number,number) don't match the method signature for SpreadsheetApp.Sheet.getRange."

I have been trying to play with getRange but always get errors

Code:

`

function splitInto1KSheets(){
  const sliceLength = 1000
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('new list');
  const values = sheet.getRange(1,1,2,sheet.getLastRow()).getValues()
  const rounds = values.length / sliceLength
  let n = 0
  

  for (let i = 0; i < rounds; i  ){
    const slice = values.slice(n, n   sliceLength)
    const newSheet = ss.insertSheet().setName(`${i 1}K`)
    newSheet.getRange(1,1,2,slice.length,1).setValues(slice)
    n  = sliceLength
  }

}

`

I was expecting this to return different sheets with 1k of rows from A to B

CodePudding user response:

You are currently reading just 2 rows of data but great many columns. Use this to get all rows and two columns instead:

  const values = sheet.getRange(1, 1, sheet.getLastRow(), 2).getValues();

Also, the parameters you give to newSheet.getRange() are incorrect. You can fix that like this:

    newSheet.getRange(1, 1, slice.length, slice[0].length).setValues(slice);
  • Related