2017-10-17 64 views
0

我是這種編程語言的新手。有人可以幫助修復我的語法嗎? 我想這個例子結合起來:正確的setBasicFilter條件語法

 function setFilter() { 
     var ss = SpreadsheetApp.getActiveSpreadsheet(); 

     var filterSettings = {}; 

     // The range of data on which you want to apply the filter. 
     // optional arguments: startRowIndex, startColumnIndex, endRowIndex, endColumnIndex 
     filterSettings.range = { 
     sheetId: ss.getActiveSheet().getSheetId() 
     }; 

     // Criteria for showing/hiding rows in a filter 
     //   https://developers.google.com/sheets/api/reference/rest/v4/FilterCriteria 
     filterSettings.criteria = {}; 
     var columnIndex = 2; 
     filterSettings['criteria'][columnIndex] = { 
     'hiddenValues': ["England", "France"] 
     }; 

     var request = { 
     "setBasicFilter": { 
      "filter": filterSettings 
     } 
     }; 
     Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId()); 
    } 

與此JSON表示:

{ 
     "hiddenValues": [ 
     string 
     ], 
     "condition": { 
     object(BooleanCondition) 
     }, 
    } 

設置這種類型的標準(這是我需要幫助,我不知道什麼樣的對象應該是):

//*original 'hiddenValues': ["England", "France"] 
     'condition': {object(NOT_BLANK)} 

我一直無法找到使用「條件」才「hiddenValues」 在互聯網上的例子...任何幫助,將不勝感激

回答

0

你正在尋找的對象是從enum(ConditionType)列表匹配你想要的過濾條件object(BooleanCondition)

{ 
    "type": enum(ConditionType), 
    "values": [{ object(ConditionValue) }], 
} 

挑選。在說明中,它會告訴你是否需要添加object(ConditionValue)

如果你需要一個或多個ConditionValues,與要麼"relativeDate" || "userEnteredValue"

// object(ConditionValue) - Pick one 
{ 
    "relativeDate": enum(RelativeDate), 
    "userEnteredValue": string, 
} 

位置添加下面的對象到values[]BooleanConditionobject(FilterCriteria),你現在有'condition': {object(NOT_BLANK)}

// object(BooleanCondition) with condition "NOT_BLANK" 
var condition_value = { 
     "type": "NOT_BLANK" 
    } 

// New FilterCriteria object 
filterSettings['criteria'][columnIndex] = { 
    'hiddenValues': ["England", "France"], 
    'condition': condition_value 
} 
+0

我裏面沒有此更改會導致更長的錯誤。但是,我似乎無法得到理想的結果。看來我嘗試過的每個方法都是過濾第二個選項卡,它想要執行第一個選項。 ...即使第二個是活動標籤。它還將過濾器應用於列C而不是列A(較深的過濾器圖標)。過濾器在那裏,但沒有條件。 –

+0

那是因爲它的'range'是'ss.getActiveSheet()。getSheetId()'。這通常是第一個標籤。你需要獲得你真正想要的工作表。有很多方法可以做到這一點。 [getSheets()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheets()),[getSheetsByName()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheetByName(String))等'ss.getSheets()[1] .getSheetId()'將得到第二個選項卡。在這種情況下,列A位於索引0處 –

+0

我剛剛發現我的編輯未保存在腳本中。我不確定是否因爲我的手機和計算機同時打開了工作表或打開了該工作表。但是,一旦我全部關閉並重新打開它,事情又開始好起來了。 –