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

Angel S

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

doubleunary

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the true range when editing multiple rows on Google Sheet via Google App Script

How do I get arrays mapped in a row in google sheet using JavaScript/Google App script

How do i get the date value from google spreadsheet database using javascript in google app script

How do I get data from a prompt or dialog and put it in a Google Doc via Google Web App Script

How do I get Figma API to work with the Google App-script API?

Google App Script: How do I get only data range from named range?

How do I send Google App Script email using HTML

How do I run custom python script in Google App engine

How do I send email to multiple recipients in google app script

How do I get jQuery mobile to work with app script?

How do I get the current file's name in Google Script?

How do I get the current instance of "this" file in Google Apps Script?

How can I open the print window (export to pdf) via Google App Script?

How do I get SoapClient working with Google App Engine (GAE)?

How do I add a script tag before the closing body tag for an A/B experiment using Google Optimize?

How do I get the URL for a Google Sheets file that I just created using the copy function - google script

How do I embed a standalone Google Apps Script web app that requires authorization into the new Google Sites?

How do I deploy a simple Google App Script Library to a Google Sheet

How to do autofocus for a google web app script?

How do i get information from my app config in my app script?

How do I get these functions to print to the screen?

How do I get a variable to print a string

How do I get an invalid message to print?

How do I get this for loop to print

Google app engine or do i need Google apps script?

Google App Script, updates to sheet are not being found by rest of script. How do I make it include updates previously done in the script?

How do I get Google search results from urlfetch in google apps script

How do I get the print leaderboard functionality to print Name: Score?

How do I get Google Apps Script to do SHA-256 encryption?