0

當執行使用Google Sheets API對工作表A至Z進行排序的代碼時,出現一個我不知道如何解決的錯誤。未指定排序順序

留言[無效請求[0] .sortRange:不排序順序指定]地址[ - ]原因[錯誤請求]域[全球]

使用此代碼,它應該已經創建排序順序和維度索引,並將其放入SortSpec中,該特性已添加到SortRange中,但它似乎不像Google表格能識別的那樣。在Visual Studio中

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(); 

使用調試,我通過汽車看了看要求有對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

答案很簡單。您不必在列表中創建單獨的排序規則,而必須定義維度索引並對相同變量排序順序

Data.SortSpec so = new Data.SortSpec(); 
so.SortOrder = "ASCENDING"; 
so.DimensionIndex = 0; 

List<Data.SortSpec> ss = new List<Data.SortSpec>(); 
ss.Add(so);