Google Sheet API batch update issue iOS

末鹿安然 提交于 2019-12-12 09:59:29

问题


Am trying to do a batch update with google sheet API for iOS but am getting an error invalid value at 'data[0].values[0]' (type.googleapis.com/google.protobuf.ListValue),

here is my code

 NSString *baseUrl = @"https://sheets.googleapis.com/v4/spreadsheets/";
NSString *spreadsheetId = @"1tJd4toWFxmHAEOeONauRPcuH_rWJOESLQT7TvCIK0x0";

baseUrl= [baseUrl stringByAppendingString:spreadsheetId];
baseUrl = [baseUrl stringByAppendingString:@"/values:batchUpdate/"];



NSMutableDictionary * params=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"USER_ENTERED",@"valueInputOption", nil];

NSURL *postURL=[GTLUtilities URLWithString:baseUrl queryParameters:params];

NSLog(@"base url is %@", postURL);

GTLObject * body=[[GTLObject alloc]init];



NSMutableArray * titleArray=[[NSMutableArray alloc]initWithObjects:@"Customer ID",@"Customer Name",@"Latitude",@"Longitude" ,@"Last_Updated_At",nil];
NSMutableArray * wheelArray2=[[NSMutableArray alloc]initWithObjects:@"rt",@"SHJ",@"150.00",@"100.00",@"2:00:00", nil];

//[contentArray addObject:titleArray];



NSMutableDictionary * batchParams=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"USER_ENTERED",@"valueInputOption", nil];


NSMutableArray * batchParametersContentArray=[[NSMutableArray alloc]init];


NSMutableDictionary*  batchParametersTitlesDict=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Sheet1!A1:E1",@"range",@"COLUMNS",@"majorDimension",titleArray,@"values", nil];


NSMutableDictionary*  batchParametersContentDict=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Sheet1!A7:E7",@"range",@"ROWS",@"majorDimension",wheelArray2,@"values", nil];


[batchParametersContentArray addObject:batchParametersTitlesDict];

[batchParametersContentArray addObject:batchParametersContentDict];

[batchParams setObject:batchParametersContentArray forKey:@"data"];


NSLog(@"batch params are %@",batchParams);


body.JSON=batchParams;


[self.service fetchObjectByInsertingObject:body forURL:postURL completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    if (error==nil) {
        NSLog(@"batch performed successfully");
    }
    else
    {
        NSLog(@"error is %@", error);
    }

}];

My Request parameters are

batch params are {
data =     (
            {
        majorDimension = COLUMNS;
        range = "Sheet1!A1:E1";
        values =             (
            "Customer ID",
            "Customer Name",
            Latitude,
            Longitude,
            "Last_Updated_At"
        );
    },
            {
        majorDimension = ROWS;
        range = "Sheet1!A7:E7";
        values =             (
            rt,
            SHJ,
            "150.00",
            "100.00",
            "2:00:00"
        );
    }
);
valueInputOption = "USER_ENTERED";
}

The error is

error is Error Domain=com.google.GTLJSONRPCErrorDomain Code=400 "(Invalid value at 'data[0].values[0]' (type.googleapis.com/google.protobuf.ListValue), "Customer ID"

Invalid value at 'data[0].values[1]' (type.googleapis.com/google.protobuf.ListValue), "Customer Name" Invalid value at 'data[0].values[2]' (type.googleapis.com/google.protobuf.ListValue), "Latitude" Invalid value at 'data[0].values[3]' (type.googleapis.com/google.protobuf.ListValue), "Longitude" Invalid value at 'data[0].values[4]' (type.googleapis.com/google.protobuf.ListValue), "Last_Updated_At" Invalid value at 'data[1].values[0]' (type.googleapis.com/google.protobuf.ListValue), "rt" Invalid value at 'data[1].values[1]' (type.googleapis.com/google.protobuf.ListValue), "SHJ" Invalid value at 'data[1].values[2]' (type.googleapis.com/google.protobuf.ListValue), "150.00" Invalid value at 'data[1].values[3]' (type.googleapis.com/google.protobuf.ListValue), "100.00" Invalid value at 'data[1].values[4]' (type.googleapis.com/google.protobuf.ListValue), "2:00:00")" UserInfo={error=Invalid value at 'data[0].values[0]' (type.googleapis.com/google.protobuf.ListValue), "Customer ID" Invalid value at 'data[0].values[1]' (type.googleapis.com/google.protobuf.ListValue), "Customer Name" Invalid value at 'data[0].values[2]' (type.googleapis.com/google.protobuf.ListValue), "Latitude" Invalid value at 'data[0].values[3]' (type.googleapis.com/google.protobuf.ListValue), "Longitude" Invalid value at 'data[0].values[4]' (type.googleapis.com/google.protobuf.ListValue), "Last_Updated_At" Invalid value at 'data[1].values[0]' (type.googleapis.com/google.protobuf.ListValue), "rt" Invalid value at 'data[1].values[1]' (type.googleapis.com/google.protobuf.ListValue), "SHJ" Invalid value at 'data[1].values[2]' (type.googleapis.com/google.protobuf.ListValue), "150.00" Invalid value at 'data[1].values[3]' (type.googleapis.com/google.protobuf.ListValue), "100.00" Invalid value at 'data[1].values[4]' (type.googleapis.com/google.protobuf.ListValue), "2:00:00", NSLocalizedFailureReason=(Invalid value at 'data[0].values[0]' (type.googleapis.com/google.protobuf.ListValue), "Customer ID" Invalid value at 'data[0].values[1]' (type.googleapis.com/google.protobuf.ListValue), "Customer Name" Invalid value at 'data[0].values[2]' (type.googleapis.com/google.protobuf.ListValue), "Latitude" Invalid value at 'data[0].values[3]' (type.googleapis.com/google.protobuf.ListValue), "Longitude" Invalid value at 'data[0].values[4]' (type.googleapis.com/google.protobuf.ListValue), "Last_Updated_At" Invalid value at 'data[1].values[0]' (type.googleapis.com/google.protobuf.ListValue), "rt" Invalid value at 'data[1].values[1]' (type.googleapis.com/google.protobuf.ListValue), "SHJ" Invalid value at 'data[1].values[2]' (type.googleapis.com/google.protobuf.ListValue), "150.00" Invalid value at 'data[1].values[3]' (type.googleapis.com/google.protobuf.ListValue), "100.00" Invalid value at 'data[1].values[4]' (type.googleapis.com/google.protobuf.ListValue), "2:00:00"), GTLStructuredError=GTLErrorObject 0x7f89ca465990: {code:400 message:"Invalid value at 'data[0].values[0]' (type.googleapis.com/google.protobuf.ListValue), "Customer ID" Invalid value at 'data[0].values[1]' (type.googleapis.com/google.protobuf.ListValue), "Customer Name" Invalid value at 'data[0].values[2]' (type.googleapis.com/google.protobuf.ListValue), "Latitude" Invalid value at 'data[0].values[3]' (type.googleapis.com/google.protobuf.ListValue), "Longitude" Invalid value at 'data[0].values[4]' (type.googleapis.com/google.protobuf.ListValue), "Last_Updated_At" Invalid value at 'data[1].values[0]' (type.googleapis.com/google.protobuf.ListValue), "rt" Invalid value at 'data[1].values[1]' (type.googleapis.com/google.protobuf.ListValue), "SHJ" Invalid value at 'data[1].values[2]' (type.googleapis.com/google.protobuf.ListValue), "150.00" Invalid value at 'data[1].values[3]' (type.googleapis.com/google.protobuf.ListValue), "100.00" Invalid value at 'data[1].values[4]' (type.googleapis.com/google.protobuf.ListValue), "2:00:00"" errors?:[1] status?:"INVALID_ARGUMENT"}}


回答1:


Values are intended to be a 2d array. They should be in the form of [[ 'a', 'b', 'c'], [ 1, 2, 3 ]]

You're supplying the values as a 1d array ['a', 'b', 'c'], and the error message is complaining the 'a' can't fit into a ListValue since it's not an array. The solution is to use a 2d array, although I can't provide an example here of exactly what you should do since I'm not sure how you intend your input to map to the spreadsheet.




回答2:


The error indicates that a request parameter has an invalid value and the error response provide the information as to which value was invalid. Do not retry without fixing the problem. You need to provide a valid value for the parameter specified in the error response. You need to make changes to the API query in order for it to work. Your Customer Name, Latitude, Longitude value was invalid. Check your error log.

Here's a Sheets API BatchUpdate blog encountered 400 error response: https://github.com/google/google-api-nodejs-client/issues/588



来源:https://stackoverflow.com/questions/38901386/google-sheet-api-batch-update-issue-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!