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

Java 使用Google电子表格API更改行颜色,java,google-sheets,google-spreadsheet-api,Java,Google Sheets,Google Spreadsheet Api,我想使用Google更改电子表格的行颜色 我正在使用JAVA,我看到它在JavaScript中工作,但在JAVA中找不到它。设置单元格颜色: 据我所知,谷歌应用程序脚本JavaScript是唯一的选择。电子表格API gdata无法实现这一点,我知道这是一个很长的时间,因为您最初提出了要求,但根据v4 API,您可以在技术上设置一个条件格式,该格式在电子表格中始终为真。batchUpdate 例如 可能不是最容易管理的事情,但在“技术上”是可能的至少可以说Google Sheet API文档不是

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

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

设置单元格颜色:
据我所知,谷歌应用程序脚本JavaScript是唯一的选择。电子表格API gdata无法实现这一点,我知道这是一个很长的时间,因为您最初提出了要求,但根据v4 API,您可以在技术上设置一个条件格式,该格式在电子表格中始终为真。batchUpdate

例如


可能不是最容易管理的事情,但在“技术上”是可能的

至少可以说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()

通过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}`;
    }
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)"
    
}
    }