Google docs formatting, converting texts into sheets

こ雲淡風輕ζ 提交于 2020-02-04 01:28:14

问题


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:

  • SCOPE must be set with a list format, each url in a different list index
  • make sure your .txt is formatted with two \n's per PAGE and single \n for next rows.
  • if for whatever reason you have invalid credentials, recheck your "credentials.json" or "client_secrets.json" before starting to panic.

I want to thank @Tanaike yet again, for his patience and dedication to my solution and for his help with debugging it.


回答1:


  • 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.

Issue and workaround:

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.

Flow:

The flow of this sample script is as follows.

  1. Retrieve the text data from the text file.
  2. Parse the text data.
    • When I saw your text data, I thought that the data can be parsed as the CSV data.
    • Here, the text data is separated for putting to each sheet.
  3. Create new Spreadsheet.
  4. Put the parsed data to the new Spreadsheet.
    • Here, each data is put to the individual sheet.

Usage:

  1. Copy and paste the following sample script to the script editor of Google Apps Script.
    • At that time, please set the file ID of the text file to fileId.
  2. Run the function of myFunction().
    • When the script is run, the authorization screen is opened. So please authorize the scopes.

By this, the script works.

Sample script:

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

Note:

  • In this sample script, new Spreadsheet is created abd the data is put to each sheet. Of course, the data can be put to the existing Spreadsheet. In that case, it is required to modify the script.

References:

  • Class DriveApp
  • Class SpreadsheetApp
  • reduce()
  • parseCsv(csv)

If I misunderstood your question and this was not the direction you want, I apologize.

Added:

  • You want to achieve above goal using google-api-python-client with python.
  • You have already been able to use the Drive API and Sheets API using python.

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.

Sample script

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)
  • When you run the script, the data is retrieved from Google Drive and the new Spreadsheet is created. In this case, the values are put to the Spreadsheet with "USER_ENTERED". By this, the values can be parsed as the string, number and date. And also, the single quote of the top of character is not used.
  • About credentials=creds, please check the Quickstart for python.


来源:https://stackoverflow.com/questions/59847515/google-docs-formatting-converting-texts-into-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!