在Google表格中使用Swift更改单元格的背景颜色。

huangapple go评论91阅读模式
英文:

Change the background color of a cell in Google sheets in swift

问题

我试图在iOS(Swift)中更改Google表格单元格的背景颜色,但我找不到任何文档。

我在GitHub上找到一个项目,允许您更改值,并且它可以正常工作。

这是代码:

@IBAction func write() {
    let range = "B2"
    let valueRange = GTLRSheets_ValueRange(json: [
        "majorDimension": "ROWS",
        "range": range,
        "values": [
            [
                10,
            ],
        ],
    ])

    let query = GTLRSheetsQuery_SpreadsheetsValuesUpdate.query(withObject: valueRange, spreadsheetId: Globals.shared.YOUR_SHEET_ID, range: range)
    query.valueInputOption = "USER_ENTERED"
    query.includeValuesInResponse = true

    Globals.shared.sheetService.executeQuery(query) { (_: GTLRServiceTicket, result: Any?, error: Error?) in
        if let error = error {
            print("Error", error.localizedDescription)
            self.view.makeToast(error.localizedDescription)
        } else {
            let data = result as? GTLRSheets_UpdateValuesResponse
            let rows = data?.updatedData?.values as? [[String]] ?? [""]
            for row in rows {
                print("row: ", row)
                self.view.makeToast("value \(row.first ?? "") wrote in \(range)")
            }
            print("success")
        }
    }
}

但我找不到如何更改背景颜色。

有什么提示吗?谢谢

英文:

I'm trying to change the background color of a google sheets cell in iOS (swift), but I can't find any documentation.

I found a project on github that allows you to change a value, and it works correctly.

This is the code:

@IBAction func write() {
        let range = "B2"
        let valueRange = GTLRSheets_ValueRange(json: [
            "majorDimension": "ROWS",
            "range": range,
            "values": [
                [
                    10,
                ],
            ],
        ])

        let query = GTLRSheetsQuery_SpreadsheetsValuesUpdate.query(withObject: valueRange, spreadsheetId: Globals.shared.YOUR_SHEET_ID, range: range)
        query.valueInputOption = "USER_ENTERED"
        query.includeValuesInResponse = true

        Globals.shared.sheetService.executeQuery(query) { (_: GTLRServiceTicket, result: Any?, error: Error?) in
            if let error = error {
                print("Error", error.localizedDescription)
                self.view.makeToast(error.localizedDescription)
            } else {
                let data = result as? GTLRSheets_UpdateValuesResponse
                let rows = data?.updatedData?.values as? [[String]] ?? [[""]]
                for row in rows {
                    print("row: ", row)
                    self.view.makeToast("value \(row.first ?? "") wrote in \(range)")
                }
                print("success")
            }
        }
    }

But I can't find how to change the background color.

Any hints? Thanks

答案1

得分: 0

以下是代码的翻译部分:

@IBAction func setBackgroundColor() {
    let sheetId = Globals.shared.YOUR_SHEET_ID
    let color = GTLRSheets_Color() 
    color.red = 0 
    color.green = 1
    color.blue = 0 
    color.alpha = 1

    let cellFormat = GTLRSheets_CellFormat()
    cellFormat.backgroundColor = color

    let updateCellsRequest = GTLRSheets_UpdateCellsRequest()
    updateCellsRequest.fields = "userEnteredFormat.backgroundColor"
    updateCellsRequest.start = GTLRSheets_GridCoordinate(json: [
        "columnIndex": 1,
        "rowIndex": 1,
        "sheetId": mySheetId,
    ])
    
    updateCellsRequest.rows = [GTLRSheets_RowData()]
    updateCellsRequest.rows![0].values = [GTLRSheets_CellData()]
    updateCellsRequest.rows![0].values![0].userEnteredFormat = cellFormat

    let batchUpdateRequest = GTLRSheets_BatchUpdateSpreadsheetRequest()
    batchUpdateRequest.requests = [GTLRSheets_Request()]
    batchUpdateRequest.requests![0].updateCells = updateCellsRequest

    let query = GTLRSheetsQuery_SpreadsheetsBatchUpdate.query(withObject: batchUpdateRequest, spreadsheetId: sheetId)
    Globals.shared.sheetService.executeQuery(query) { (_, result, error) in
        if let error = error {
            print("Error", error.localizedDescription)
            self.view.makeToast(error.localizedDescription)
        } else {
            print("success")
        }
    }
}

希望这有所帮助。如果您有任何其他问题,请随时提出。

英文:

Found the answer:

 @IBAction func setBackgroundColor() {
    let sheetId = Globals.shared.YOUR_SHEET_ID
    let color = GTLRSheets_Color() 
    color.red = 0 
    color.green = 1
    color.blue = 0 
    color.alpha = 1

    let cellFormat = GTLRSheets_CellFormat()
    cellFormat.backgroundColor = color

    let updateCellsRequest = GTLRSheets_UpdateCellsRequest()
    updateCellsRequest.fields = "userEnteredFormat.backgroundColor"
    updateCellsRequest.start = GTLRSheets_GridCoordinate(json: [
        "columnIndex": 1,
        "rowIndex": 1,
        "sheetId": mySheetId,
    ])
    
    

    updateCellsRequest.rows = [GTLRSheets_RowData()]
    updateCellsRequest.rows![0].values = [GTLRSheets_CellData()]
    updateCellsRequest.rows![0].values![0].userEnteredFormat = cellFormat

    let batchUpdateRequest = GTLRSheets_BatchUpdateSpreadsheetRequest()
    batchUpdateRequest.requests = [GTLRSheets_Request()]
    batchUpdateRequest.requests![0].updateCells = updateCellsRequest

    let query = GTLRSheetsQuery_SpreadsheetsBatchUpdate.query(withObject: batchUpdateRequest, spreadsheetId: sheetId)
    Globals.shared.sheetService.executeQuery(query) { (_, result, error) in
        if let error = error {
            print("Error", error.localizedDescription)
            self.view.makeToast(error.localizedDescription)
        } else {
            print("success")
        }
    }
}

huangapple
  • 本文由 发表于 2023年3月15日 21:13:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定