使用Google SpreadSheet API更新iOS Swift中的特定行

时间:2018-07-29 22:23:47

标签: ios iphone swift google-sheets google-sheets-api

我一直在使用iOS Swift中的Google SpreadSheet API,但是在更新iOS Swift中SpreadSheet行/数据中的特定字段时出现了问题,

以下是在电子表格顶部添加一行的代码,但是我想要一个特定的索引或匹配一个特定的值,

  let service = GTLRSheetsService()
            service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
            service.apiKey = Constants.GOOGLE_SPEADSHEET_API_KEY
            let range = "Employee List!A2:D"
             let valueRange = GTLRSheets_ValueRange.init()
             valueRange.values = [[job.jobTitle!]] //want to add job title on specific field after maching employee name

            let query = GTLRSheetsQuery_SpreadsheetsValuesUpdate.query(withObject: valueRange, spreadsheetId:  Constants.SPREAD_SHEET_ID, range: range)
    query.valueInputOption = "USER_ENTERED"

    service.executeQuery(query) { (ticket, response, error) in
        print(response)

    }

此代码将作业标题添加到B2列的顶部,但是我想通过检查名称并将其添加到特定行中,然后在该行上添加作业。

PS:数据看起来像这样

Want to add job title by matching the particular employee name, ref to image

有人可以帮我吗?

非常感谢您

1 个答案:

答案 0 :(得分:0)

您应为要更新的单元格定义确切范围。可能看起来像这样:

let service = GTLRSheetsService()
service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
service.apiKey = Constants.GOOGLE_SPEADSHEET_API_KEY
let rowIndex:Int = job.rowIndex
let range = "Employee List!C\(rowIndex):C\(rowIndex)"
let valueRange = GTLRSheets_ValueRange()
valueRange.range = "Employee List!C\(rowIndex):C\(rowIndex)"
valueRange.values = [[job.jobTitle!]]
valueRange.majorDimension = "COLUMNS"

let query = GTLRSheetsQuery_SpreadsheetsValuesUpdate.query(withObject: valueRange, spreadsheetId:  Constants.SPREAD_SHEET_ID, range: range)
query.valueInputOption = "USER_ENTERED"

service.executeQuery(query) { (ticket, response, error) in
    print(response)

}

不要忘记在scope

中定义权限GIDSignIn.sharedInstance().scopes