I'm importing text data into Sheets and need to split it via a defined fixed width. I know the column width limit, which is where I'm trying to define column breaks as the data will not be wider than that for each column, however it does not have a consistent delimiter.
I've done this previously in Excel using the following VBA code:
Workbooks.OpenText Filename:=sFileName, Origin:=437, StartRow:=41,
DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), _
Array(23, 1), _
Array(34, 1), _
Array(59, 1), _
Array(70, 1), _
Array(79, 1), _
Array(87, 1), _
Array(98, 1), _
Array(114, 1), _
Array(123, 1)), _
TrailingMinusNumbers:=True
I've got a code setup to pull a text file into the workbook and I attempted to split using a SLICE
function but it gave an error that the range had ~4000 rows by the data had 0 rows so it couldn't generate.
function importTXTfromDrive() {
var fileName = Browser.inputBox("Enter the name of the text file in your
Google Drive to import (e.g. myFile.csv):");
var searchTerm = "title = '"+fileName+"'";
var files = DriveApp.searchFiles(searchTerm)
var csvFile = "";
while (files.hasNext()) {
var file = files.next();
if (file.getName() == fileName) {
csvFile = file.getBlob().getDataAsString();
break;
}
}
var csvData = Utilities.parseCsv(csvFile);
////var col1 = csvData.slice(23,1);
////This didn't work so I removed it for now
var ss = SpreadsheetApp.getActiveSpreadsheet();
if (ss.getSheetByName(fileName) != null){
Browser.msgBox("That file has already been opened");
return 0;
}
var create = ss.insertSheet(fileName);
create.getRange(1, 1, csvData.length,
csvData[0].length).setValues(csvData);
////create.getRange(1, 1, csvData.length,
////csvData[0].length).setValues(col1);
////This last line was a test in using the SLICE but gave an error
}