I've had success in sending a google docs file into a google sheets and the spreadsheet managed to fill itself in correctly (it formats the text into a table, very neat)
Below is an example of how I'm formatting my text:
ID, NAME, MOBILE, CITY, COUNTRY, BIRTHDAY,
3, NameGoesHere1, 21 98658 5548, abcity, countryNameHere, 1998-05-02,
6, SomeoneElse Joined Here, 21 98535 1218, whereland, Far far away, 1989-11-15,
5, AnotherCustomer, 21 85482 5245, somecity, Somewhereland, 1999-08-04,
ID, PRICE, STOCK, ASDF, BASDF, CASDF,
ID, NAME, PRICE, DESCRIPTION,
2, pen, 1.5, The pen is mightier than the sword,
3, pencil, 1.0, Can be used to write,
4, RPG, 150.0, well that escalated quickly, huh,
EMPTY,
names,
goofs,
ID, FLAVOR,
(Note that there's two empty tables, the one with "ID, PRICE, STOCK, ASDF, BASDF, CASDF" and "ID, FLAVOR" as their columns, they've been left blank on purpose)
By filling a cell with:
=IMPORTDATA("<<< <<<google drive url containing text goes here, just copy-paste>>> >>>")
I am able to create a several tables with that chosen cell being the top-left of the first table, and that command fits them all into the google sheets format automatically. Each table being placed below the previous one, starting by the column names.
I'm looking for a way to get each table in a different google-sheet page, like this:
page1:
ID, NAME, MOBILE, CITY, COUNTRY, BIRTHDAY,
3, NameGoesHere1, 21 98658 5548, abcity, countryNameHere, 1998-05-02,
6, SomeoneElse Joined Here, 21 98535 1218, whereland, Far far away, 1989-11-15,
5, AnotherCustomer, 21 85482 5245, somecity, Somewhereland, 1999-08-04,
page2:
ID, PRICE, STOCK, ASDF, BASDF, CASDF,
page3:
ID, NAME, PRICE, DESCRIPTION,
2, pen, 1.5, The pen is mightier than the sword,
3, pencil, 1.0, Can be used to write,
4, RPG, 150.0, well that escalated quickly, huh,
and so on.
I would like to know if there's a way to separate text into different pages like shown above, or what's an alternate way to convert the filetype from text to sheets such that I could split it up into different pages
ps.: I need the code to be in python, as I'm working with pydrive for the file uploads
edit.: SOLVED by @Tanaike. Important debugging to look out for @Tanaike's solution:
I want to thank @Tanaike yet again, for his patience and dedication to my solution and for his help with debugging it.
You have a text file like below. This text file is put in your Google Drive. You know the file ID of the text file.
ID, NAME, MOBILE, CITY, COUNTRY, BIRTHDAY,
3, NameGoesHere1, 21 98658 5548, abcity, countryNameHere, 1998-05-02,
6, SomeoneElse Joined Here, 21 98535 1218, whereland, Far far away, 1989-11-15,
5, AnotherCustomer, 21 85482 5245, somecity, Somewhereland, 1999-08-04,
ID, PRICE, STOCK, ASDF, BASDF, CASDF,
ID, NAME, PRICE, DESCRIPTION,
2, pen, 1.5, The pen is mightier than the sword,
3, pencil, 1.0, Can be used to write,
4, RPG, 150.0, well that escalated quickly, huh,
EMPTY,
names,
goofs,
ID, FLAVOR,
You want to put the values to the individual sheet by separating the text data every empty row.
Tab 1
ID, NAME, MOBILE, CITY, COUNTRY, BIRTHDAY,
3, NameGoesHere1, 21 98658 5548, abcity, countryNameHere, 1998-05-02,
6, SomeoneElse Joined Here, 21 98535 1218, whereland, Far far away, 1989-11-15,
5, AnotherCustomer, 21 85482 5245, somecity, Somewhereland, 1999-08-04,
Tab 2
ID, PRICE, STOCK, ASDF, BASDF, CASDF,
Tab 5
ID, FLAVOR,
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
I thought that your goal is difficult to be achieved or might not be able to be achieved by only the built-in functions of Spreadsheet. So in this answer, I would like to propose to achieve your goal using Google Apps Script.
The flow of this sample script is as follows.
fileId
.myFunction()
.
By this, the script works.
function myFunction() {
var fileId = "###"; // Please set the file ID of text file.
var newSpreadsheetName = "sampleSpreadsheet"; // Please set new Spreadsheet name.
var data = DriveApp.getFileById(fileId).getBlob().getDataAsString();
var temp = [];
var parsedData = Utilities.parseCsv(data).reduce(function(ar, e, i, d) {
if (e.join("")) {
temp.push(e);
} else {
ar.push(temp);
temp = [];
}
if (i == d.length - 1) ar.push(temp);
return ar;
}, []);
var ss = SpreadsheetApp.create(newSpreadsheetName);
parsedData.forEach(function(e, i) {
var sheet = i == 0 ? ss.getSheets()[0] : ss.insertSheet();
sheet.getRange(1, 1, e.length, e[0].length).setValues(e);
})
}
If I misunderstood your question and this was not the direction you want, I apologize.
I could understand like above. In this case, Drive API and Sheets API are used. Drive API retrieves the data from the text file on Google Drive. Sheets API creates new Spreadsheet using the values. In this case, in order to use Sheets API, I used google-api-python-client. By this, I also used Drive API with google-api-python-client.
fileId = '###' # Please set the file ID of text file.
sheets = build('sheets', 'v4', credentials=creds)
drive = build('drive', 'v3', credentials=creds)
# Retrieve data from Google Drive and parse data as an array.
data = drive.files().get_media(fileId=fileId).execute()
csvData = [row.split(",") for row in str(data, 'utf-8').split("\n")]
ar = []
temp = []
for i, row in enumerate(csvData):
if "".join(row) != "":
row = [v.strip() for v in row]
temp.append(row)
else:
ar.append(temp)
temp = []
if i == len(csvData) - 1:
ar.append(temp)
sheetsObj = []
valuesUpdateReq = []
for i, sheet in enumerate(ar):
if bool(sheet):
sheetName = "Sheet" + str(i + 1)
sheetsObj.append({"properties": {"title": sheetName}})
valuesUpdateReq.append({"values": sheet, "range": sheetName, "majorDimension": "ROWS"})
# Request to Sheets API.
body = {"properties": {"title": "sampleSpreadsheet"}, "sheets": sheetsObj}
res1 = sheets.spreadsheets().create(body=body).execute()
batch_update_values_request_body = {"data": valuesUpdateReq, "valueInputOption": "USER_ENTERED"}
res2 = sheets.spreadsheets().values().batchUpdate(spreadsheetId=res1["spreadsheetId"], body=batch_update_values_request_body).execute()
print(res2)
credentials=creds
, please check the Quickstart for python.