0

I have created Google sheet having two columns A & B. I want to enter "In time" in column A and its corresponding Out time in column B.

I have created two python scripts which append data to the columns separately. The problem is that both scripts append the data to column A only.

So While running python script for appending data to column B, I want it append data to just column B. Please guide me.

Here is the code to append data to column B only:

spreadsheet_id='sheetID'
range_name='Sheet1!B:B'
value_input_option='RAW'
values = [
    [
       str(datetime.datetime.now())
    ],
    # Additional rows ...
]

body = { 
  "majorDimension": "ROWS",
  'values': values
}

result = service.spreadsheets().values().append(
    spreadsheetId=spreadsheet_id, range=range_name,
    valueInputOption=value_input_option, body=body).execute()

print('{0}'.format(result.get('updates').get('updatedCells')))

Here is the Google Sheet which I want to create:

+------------------+------------------+
| IN TIME          | OUT TIME         |
+------------------+------------------+
| 12/01/19 [10:01] | 12/01/19 [18:01] | 
+------------------+------------------+
| 13/01/19 [10:30] | 13/01/19 [20:01] |
+------------------+------------------+
| 15/01/19 [09:45] | 15/01/19 [17:01] | 
+------------------+------------------+

Here is the Google Sheet which is getting created with the current code:

+------------------+----------+
| IN TIME          | OUT TIME | 
+------------------+----------+
| 12/01/19 [10:01] |          | 
+------------------+----------+
| 12/01/19 [18:01] |          | 
+------------------+----------+
| 13/01/19 [10:30] |          | 
+------------------+----------+
| 13/01/19 [20:01] |          | 
+------------------+----------+
| 15/01/19 [09:45] |          |
+------------------+----------+
| 15/01/19 [17:01] |          |
+------------------+----------+
  • From your tags, do you want to convert the python script to Google Apps Script? – Tanaike Mar 17 at 22:15
  • No. That tag was added by the moderator. I just want to know the Python code. – jonny789 Mar 18 at 2:22
  • Thank you for replying. You want to achieve your question using python script. I could understand like this. In order to correctly understand your issue, can I ask you about While appending to column B only, I want to skip column A & append to column B directly.? I couldn't understand about this. – Tanaike Mar 18 at 23:53
  • The above code is appending data to the first column i.e. A , not to the column B. Even though I have set range_name='Sheet1!B:B. How could I skip column A & append to column B only ? – jonny789 Mar 19 at 2:50
  • Although I'm not sure whether I could understand correctly what you want, is this the result you want? values = [["", ''.join(sys.argv[1:])]] In this case, the values are appended to the last row and the column A is empty. – Tanaike Mar 19 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.