2017-02-22 82 views
0

enter image description here條件格式與pygsheets爲谷歌牀單API

我與http://pygsheets.readthedocs.io/en/latest/index.html工作圍繞谷歌的API張V4的包裝。我有興趣使用google-sheets-api v4設置條件格式。我試圖使用自定義公式來突出顯示基於行中「Q」列的值的行。如果q列包含'TRASH',我想將該行着色爲紅色。

正如我期待通過https://github.com/nithinmurali/pygsheets/blob/master/pygsheets/client.py的pygheets圖書館中,我遇到了,我相信這是發送這種請求的方式:

# @TODO use batch update more efficiently 
def sh_batch_update(self, spreadsheet_id, request, fields=None, batch=False): 
    if type(request) == list: 
     body = {'requests': request} 
    else: 
     body = {'requests': [request]} 
    final_request = self.service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body, 
                  fields=fields) 
return self._execute_request(spreadsheet_id, final_request, batch) 

此外,在https://developers.google.com/sheets/api/samples/conditional-formatting#add_a_custom_formula_rule_to_a_range,一個例子是關於如何發送自定義給出請求。在此基礎上,我有:

import tkinter as tk 
import tkFileDialog 
import pygsheets 

def cfr1(sheetId): 

    # Apply to range: A1:R 
    # Format cells if...: Custom formula is 
    # (formula:) =$Q1="TRASH" 

    return {"requests": [ 
     { 
      "addConditionalFormatRule": { 
      "rule": { 
       "ranges": [ 
       { 
        "sheetId": sheetId, 
        "startColumnIndex": 'A', 
        "endColumnIndex": 'R', 
        "startRowIndex": 1, 
        "endRowIndex": 8 
       } 
       ], 
       "booleanRule": { 
       "condition": { 
        "type": "CUSTOM_FORMULA", 
        "values": [ 
        { 
         "userEnteredValue": '=$Q1="TRASH"' 
        } 
        ] 
       }, 
       "format": { 
        "backgroundColor": { 
          "red": 1.0 
          # "green": 0.0, 
          # "blue": 0.0 
         } 


       } 
       } 
      }, 
      "index": 0 
      } 
     } 
     ] 
    } 

root = tk.Tk() 
root.withdraw() 
file_path = tkFileDialog.askopenfilename() 
print file_path 
file_name = file_path.split('/')[-1] 
print file_name 
file_name_segments = file_name.split('_') 
spreadsheet = file_name_segments[0] 
worksheet = file_name_segments[1]+'_'+file_name_segments[2] 
print worksheet 
print spreadsheet 

gc = pygsheets.authorize(outh_file='client_secret_xxx.apps.googleusercontent.com.json') 
a =gc.list_ssheets() 
wb_list = [d['name'] for d in a] 
print wb_list 
if spreadsheet not in wb_list: 
    print "Doesn't exist .." 
else: 
    ssheet = gc.open(spreadsheet) 
    print ssheet.title 
    print 'ws '+worksheet 
    ws = ssheet.worksheet('title',worksheet) 

    gc.sh_batch_update(ssheet.id,cfr1(ws.id),'A1:R8') 

,但我發現:

googleapiclient.errors.HttpError: <HttpError 400 when requesting htps://sheets.googleapis.com/v4/spreadsheets/1Ypb_P**********pFt_SE:batchUpdate?fields=A1%3AR 
8&alt=json returned "Invalid JSON payload received. Unknown name "requests" at 'requests[0]': Cannot find field."> 

我在做什麼錯?

回答

1

在Json有效負載return {"requests": [中,您不需要密鑰requests作爲sh_batch_update中的內容,它已經將其包裝在請求中。看看worksheet.pydelete_cols的實現以查看示例用法。

那麼有效,你可以這樣做,

return { 
      "addConditionalFormatRule": { 
      "rule": { 

還你不需要路過這裏的範圍gc.sh_batch_update(ssheet.id,cfr1(ws.id),'A1:R8')領域PARAM決定響應領域返回

+0

謝謝你,讓你建議更改後,我得到「無效的價值在'請求[0] .add_conditional_format_rule.rule.ranges [0] .end_column_index.value'(TYPE_INT32),」R「 '請求[0] .add_conditional_format_rule.rule.ranges [ 0] .start_column_index.value'(TYPE_INT32),「A」「>。在這種情況下,我真正想要做的是將格式應用於表格中的每一行,但無法在文檔中找到如何操作。 – user61629

+0

對不起,我的壞,我意識到你必須使用列號,而不是字母, – user61629

+0

如果我可以問一個快速跟進。你如何對整張紙進行更改? – user61629