我试图使用google sheet API for iOS进行批量更新,但在'data [0] .values [0]'(type.googleapis.com/google.protobuf.ListValue)上收到错误无效值,
这是我的代码
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);
}
}];
我的请求参数是
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";
}
错误是
error is Error Domain=com.google.GTLJSONRPCErrorDomain Code=400 "(Invalid value at 'data[0].values[0]' (type.googleapis.com/google.protobuf.ListValue), "Customer ID"
'data [0] .values [1]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“客户名称”'data [0] .values [2]'的值无效(type.googleapis .com / google.protobuf.ListValue),“纬度”'data [0] .values [3]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“经度”数据的值无效[ 0] .values [4]'(type.googleapis.com/google.protobuf.ListValue),“Last_Updated_At”'data [1] .values [0]'的值无效(type.googleapis.com/google.protobuf . ListValue),“rt”'data [1] .values [1]'(type.googleapis.com/google.protobuf.ListValue)中的值无效,“SHJ”'data [1] .values [2]中的值无效'(type.googleapis.com/google.protobuf.ListValue),“150.00”'data [1] .values [3]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“100.00”无效'data [1] .values [4]'(type.googleapis.com/google.protobuf.ListValue),“2:00:00”)的值“UserInfo = {error ='data [0]处的值无效 . 值[0]'(type.googleapis.com/google.protobuf.ListValue),“客户ID”无效的值e在'data [0] .values [1]'(type.googleapis.com/google.protobuf.ListValue),“客户名称”'data [0] .values [2]'中的值无效(type.googleapis . com / google.protobuf.ListValue),“纬度”'data [0] .values [3]'(type.googleapis.com/google.protobuf.ListValue)中的值无效,“经度”数据的值无效[0 ] .values [4]'(type.googleapis.com/google.protobuf.ListValue),“Last_Updated_At”'data [1] .values [0]'的值无效(type.googleapis.com/google.protobuf.ListValue ),“rt”'data [1] .values [1]'(type.googleapis.com/google.protobuf.ListValue)的值无效,“SHJ”'data [1] .values [2]'的值无效(type.googleapis.com/google.protobuf.ListValue),“150.00”'data [1] .values [3]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“100.00”无效值在'data [1] .values [4]'(type.googleapis.com/google.protobuf.ListValue),“2:00:00”,NSLocalizedFailureReason =('data [0] .values [0]处的值无效'(type.googleapis.com/google.protobuf.ListValue),“客户ID”无效值a t'数据[0] .values [1]'(type.googleapis.com/google.protobuf.ListValue),“客户名称”'data [0] .values [2]'的值无效(type.googleapis.com /google.protobuf.ListValue),“纬度”'data [0] .values [3]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“经度”'data [0]处的值无效.values [4]'(type.googleapis.com/google.protobuf.ListValue),“Last_Updated_At”'data [1] .values [0]'的值无效(type.googleapis.com/google.protobuf.ListValue) ,“rt”'data [1] .values [1]'(type.googleapis.com/google.protobuf.ListValue)中的值无效,“SHJ”'data [1] .values [2]'中的值无效( type.googleapis.com/google.protobuf.ListValue),“150.00”'data [1] .values [3]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“100.00”无效值at at 'data [1] .values [4]'(type.googleapis.com/google.protobuf.ListValue),“2:00:00”),GTLStructuredError = GTLErrorObject 0x7f89ca465990:{code:400 message:“atvalid value at” data [0] .values [0]'(type.googleapis.com/google.protobuf . ListValue),“客户ID”无效值'data [0] .values [1]'(type.googleapis.com/google.protobuf.ListValue),“客户名称”无效值'data [0] .values [ 2]'(type.googleapis.com/google.protobuf.ListValue),“纬度”'data [0] .values [3]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“经度“'data [0] .values [4]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,”Last_Updated_At“'data [1] .values [0]'的值无效(type.googleapis .com / google.protobuf.ListValue),“rt”'data [1] .values [1]'(type.googleapis.com/google.protobuf.ListValue)上的值无效,“SHJ”'data'处的值无效[ 1] .values [2]'(type.googleapis.com/google.protobuf.ListValue),“150.00”'data [1] .values [3]'的值无效(type.googleapis.com/google.protobuf . ListValue),“100.00”'data [1] .values [4]'(type.googleapis.com/google.protobuf.ListValue),“2:00:00”“错误?:[1]状态的值无效? :“无效的论点”}}
2 回答
值旨在成为二维数组 . 它们应该是[['a','b','c'],[1,2,3]的形式
您将值作为1d数组['a','b','c']提供,错误消息是抱怨'a'不适合ListValue因为它不是一个数组 . 解决方案是使用2d数组,虽然我不能在这里提供你应该做的确切的例子,因为我不确定你打算如何将输入映射到电子表格 .
该错误表示请求参数具有无效值,并且错误响应提供有关哪个值无效的信息 . 如果不解决问题,请勿重试 . 您需要为错误响应中指定的参数提供有效值 . 您需要对API查询进行更改才能使其正常工作 . 您的
Customer Name, Latitude, Longitude
值无效 . 检查错误日志 .这是一个Sheets API BatchUpdate博客遇到400错误响应:https://github.com/google/google-api-nodejs-client/issues/588