I have a CSV file that is provided to me which is over 100mb. I want to convert this into a google sheet but i know the limitations of Google Sheets are as follows:
- Maximum Cells: 5 million cells per sheet, meaning a sheet can have a maximum of 5 million rows and 1 column, 1 million rows and 5 columns, and so on.
- Maximum Sheets: 200 sheets per spreadsheet.
- Maximum File Size: 50MB for a single sheet.
So what i want to do is import this csv as a blob and then break it out into 10mb segments and create new google sheets for each piece.
I have this code so far:
function splitFileIntoChunks(fileId) {
try {
// Get the file by ID
var file = DriveApp.getFileById(fileId);
// Define the maximum chunk size in bytes (10MB)
var chunkSizeBytes = 10 * 1024 * 1024;
// Get the file's size
var fileSizeBytes = file.getSize();
// Create a folder to store the chunks
var folder = DriveApp.createFolder("FileChunks_" + file.getName());
// Calculate the number of chunks needed
var numChunks = Math.ceil(fileSizeBytes / chunkSizeBytes);
// Read and split the file into chunks
var blob = file.getBlob(); // Get the Blob object once
var bytePosition = 0; // Initialize the reading position
for (var i = 0; i < numChunks; i++) {
var chunkSize = Math.min(chunkSizeBytes, fileSizeBytes - bytePosition); // Adjust for last chunk
var chunkData = blob.getBytes(chunkSize);
var chunkBlob = Utilities.newBlob(chunkData);
// Create a new file for the chunk in the folder
var chunkFileName = file.getName() + "_chunk_" + (i + 1) + ".part";
var chunkFile = folder.createFile(chunkBlob.setName(chunkFileName));
bytePosition += chunkSize; // Update position for the next chunk
}
// Optionally, return a status or message
return "File split into chunks successfully";
} catch (e) {
// Handle any errors here
console.log("Error: " + e.toString());
return "Error: " + e.toString();
}
}
when it gets to this line:
var chunkData = blob.getBytes(chunkSize);
I get an error:
Error: Exception: The parameters (number) don’t match the method
signature for Blob.getBytes.
Any ideas what I maybe doing wrong here?