英文:
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")
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论