执行使用Google表格API的代码将表格A分类到Z时,我收到一条错误,我不知道如何修复。
消息[无效请求[0] .sortRange:未指定排序顺序。]位置[ - ]原因[badRequest]域[全局]
使用此代码,它应该已经创建了排序顺序和维度索引,并将其放入SortSpec中,SortSpec已添加到SortRange中,但似乎Google表格无法识别。
List<Data.Request> requests = new List<Data.Request>(); // TODO: Update placeholder value.
Data.SortSpec so = new Data.SortSpec();
so.SortOrder = "ASCENDING";
Data.SortSpec di = new Data.SortSpec();
di.DimensionIndex = 0;
List<Data.SortSpec> ss = new List<Data.SortSpec>();
ss.Add(so);
ss.Add(di);
var test = new Request()
{
SortRange = new SortRangeRequest()
{
Range = new GridRange()
{
SheetId = 0,
StartRowIndex = 1
},
SortSpecs = ss
}
};
requests.Add(test);
// TODO: Assign values to desired properties of `requestBody`:
Data.BatchUpdateSpreadsheetRequest requestBody = new Data.BatchUpdateSpreadsheetRequest();
requestBody.Requests = requests;
SpreadsheetsResource.BatchUpdateRequest request = service.Spreadsheets.BatchUpdate(requestBody, spreadsheetId);
Data.BatchUpdateSpreadsheetResponse response = request.Execute();
在Visual Studio中使用调试,我查看了Autos以查看请求对SortSpecs有什么值,我发现两个值都存在。
- [0] {Google.Apis.Sheets.v4.Data.SortSpec} Google.Apis.Sheets.v4.Data.SortSpec
DimensionIndex null int?
ETag null string
SortOrder "ASCENDING" string
- [1] {Google.Apis.Sheets.v4.Data.SortSpec} Google.Apis.Sheets.v4.Data.SortSpec
DimensionIndex 0 int?
ETag null string
SortOrder null string
答案 0 :(得分:0)
答案非常简单。您不必在列表中创建单独的sortspec,而是必须将维度索引和排序顺序定义到同一个变量
Data.SortSpec so = new Data.SortSpec();
so.SortOrder = "ASCENDING";
so.DimensionIndex = 0;
List<Data.SortSpec> ss = new List<Data.SortSpec>();
ss.Add(so);