使用Google电子表格API更改行颜色

时间:2014-08-04 17:18:20

标签: java google-sheets google-spreadsheet-api

我想使用Google Spreadsheet API更改电子表格的行颜色。

我正在使用 JAVA ,我看到它在 JavaScript 中工作,但我没有在 JAVA 中找到它。

5 个答案:

答案 0 :(得分:3)

Google Sheet API文档不是最好的说法,但在这里摆弄了一些有效的python代码之后:

http = credentials.authorize(httplib2.Http())
discovery_url = ('https://sheets.googleapis.com/$discovery/rest?'
                 'version=v4')
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discovery_url, cache_discovery=False)

spreadsheet = service.spreadsheets().get(spreadsheetId=ss.id).execute()

requests = []

for sheet in spreadsheet.get('sheets'):
    sheetId = sheet.get('properties').get('sheetId')
    requests.append({
            "updateCells": {
            "rows": [
                {
                    "values": [{
                                   "userEnteredFormat": {
                                       "backgroundColor": {
                                           "red": 1,
                                           "green": 0,
                                           "blue": 0,
                                           "alpha": 1
                                       }}}
                    ]
                }
            ],
            "fields": 'userEnteredFormat.backgroundColor',
            "range": {
                "sheetId": sheetId,
                "startRowIndex": 0,
                "endRowIndex": 1,
                "startColumnIndex": 0,
                "endColumnIndex": 1
            }}})

    body = {
        'requests': requests
    }

    response = service.spreadsheets().batchUpdate(spreadsheetId=ss.id, body=body).execute()

答案 1 :(得分:2)

我知道这是你最初问过的很长时间,但根据v4 API,你可以在技术上设置一个条件格式,oncreate

总是如此

例如。 https://developers.google.com/sheets/api/samples/conditional-formatting

可能不是最容易管理的东西,但在技术上是“#39;可能

答案 2 :(得分:1)

设置单元格颜色:

https://developers.google.com/apps-script/reference/spreadsheet/range#setBackground(String)

据我所知,google-apps-script(JavaScript)是唯一的选择。它不能使用电子表格API(gdata)

完成

答案 3 :(得分:1)

object = {
    
"updateCells": {
   "range": {
        "sheetId": sheetId,
    
         "startRowIndex":startRowIndex,
         "endRowIndex": endRowIndex,
    
         "startColumnIndex": startColumnIndex,
                
         "endColumnIndex": endColumnIndex
    
        }
  "rows": [{
      "values": [{
          "textFormatRuns": [
     
              {"format": {
    
                "foregroundColor": {
     
                   "red": 0.0,
    
                   "green": 255.0,
    
                   "blue": 31.0
    
                },
    
             },"startIndex": 0
     
         },
    
       ]
    
     }
    
    ]
    
  }]
"fields": "textFormatRuns(format)"
    
}
    }

答案 4 :(得分:0)

使用JavaScript API,您可以使用以下代码:

    const range = {
      sheetId: 250062959, // find your own
      startRowIndex: 0,
      endRowIndex: 1,
      startColumnIndex: 0,
      endColumnIndex: 1,
    };

    const request = {
      spreadsheetId, // fill with your own
      resource: {
        requests: [
          {
            updateCells: {
              range,
              fields: '*',
              rows: [
                {
                  values: [
                    {
                      userEnteredValue: { stringValue: 'message' },
                      userEnteredFormat: {
                        backgroundColor: { red: 1, green: 0, blue: 0 },
                      },
                    },
                  ],
                },
              ],
            },
          },
        ],
      },
    };

    try {
      const result = await client.spreadsheets.batchUpdate(request);
      console.log(result);
    } catch (error) {
      throw `update row error ${error}`;
    }