Updating specific row in iOS Swift using Google Sp

2019-03-06 06:52发布

I have been working on Google SpreadSheet API in iOS swift, but theres a problem in updating a specific field in SpreadSheet row/data in iOS Swift,

Following is the code that adds one row on the top of spreadsheet, But I want on a particular index or matching a particular value,

  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)

    }

This Code adds job title on the top of B2 column, but I want to add this on a specific row, by checking the name and then add job against it.

PS: The data looks like this

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

Can somebody help me out?

Thank you very much

1条回答
祖国的老花朵
2楼-- · 2019-03-06 07:06

You should define an exact range for a cell you are going to update. It might look like this:

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)

}

And don't forget to define a right scope in GIDSignIn.sharedInstance().scopes

查看更多
登录 后发表回答