Reputation: 116
I am using gspread and I am looking for a proper way to changing column format by script. I have a column with time durations. I would like to change format of entire column to duration
. In Google UI I can mark entire column then click format, then number and set duration. Is it possible to do it by gspread/google sheets API?
EDIT
client = gspread.authorize(credentials)
try:
sheet = client.open(sys.argv[1]).sheet1
except (gspread.SpreadsheetNotFound, IndexError):
print("Spreadsheet not found")
sys.exit()
try:
tags = sheet.col_values(13)
tags.remove('Tags')
unique_tags = list(dict.fromkeys(tags))
except ValueError:
print("Your spreadsheet cannot be modified and should contain original
columns from toggle reports.")
sys.exit()
START_INDEX = 7
sheet.update_cell(6, 15, "SUM")
for tag in unique_tags:
sheet.update_cell(START_INDEX, 15, tag)
sheet.update_cell(START_INDEX, 16, "=SUMIF(M2:M; " + '"' + tag + '"' + ";
L2:L)")
START_INDEX += 1
sheet.update_cell(6, 16, "=SUM(P7:P15)")
Upvotes: 2
Views: 4745
Reputation: 201378
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
batch_update(body)
.In this modification, the top part of script in your question is used.
try:
spreadsheet = client.open(sys.argv[1]) # Modified
sheet = spreadsheet.sheet1 # Added
except (gspread.SpreadsheetNotFound, IndexError):
print("Spreadsheet not found")
sys.exit()
# I added below script.
requests = [{
"repeatCell": {
"range": {
"startColumnIndex": 0,
"endColumnIndex": 1,
"sheetId": sheet._properties['sheetId']
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "TIME",
"pattern": "[m]:s"
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
}]
body = {
'requests': requests
}
res = spreadsheet.batch_update(body)
print(res)
1:23:45
as the time duration, when above script (the format pattern is [m]:s
.) is run, 1:23:45
becomes 83:45
.
1:23:45
as the time duration is [h]:mm:ss
.[s]
, 1:23:45
becomes 5025
.sys.argv[1]
is changed to [m]:s
.For the pattern of time duration, the official document says as follows.
[h+]
: Number of elapsed hours in a time duration. Number of letters indicates minimum number of digits (adds leading 0s).[m+]
: Number of elapsed minutes in a time duration. Number of letters indicates minimum number of digits (adds leading 0s).[s+]
: Number of elapsed seconds in a time duration. Number of letters indicates minimum number of digits (adds leading 0s).
Upvotes: 2