How to write the MIN, MID, MAX enums in Google She

2019-08-19 02:29发布

问题:

How can I use an InterpolationPointType enum in the following script?

See the commented lines with the hand pointers to see where I'm hitting an error: Invalid value at 'requests[0].add_conditional_format_rule.rule.gradient_rule.midpoint.type' (TYPE_ENUM)

I'm excited about what I'm designing here and think I'm close to achieving a script that can dynamically add color gradient conditional formatting to a Google Sheet.

But I haven't found a way around this enum problem even when looking at docs for ConditionalFormatRule, Gradient rules, and Add a conditional color gradient across a row.

function addGradients(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName("temp");
  var requests = [];

  //Do this for as many ranges as needed:
  var myRange = Sheets.newGridRange();
  myRange.sheetId = sheet.getSheetId();
  myRange.startRowIndex = 12;
  myRange.endRowIndex = 24;
  myRange.startColumnIndex = 4;
  myRange.endColumnIndex = 4;  
  var request1 = addGradientToRange(myRange);
  requests.push(request1);

  // Batch send the requests  
  var batchUpdate = Sheets.newBatchUpdateSpreadsheetRequest();
  batchUpdate.requests = requests;
  var response = Sheets.Spreadsheets.batchUpdate(batchUpdate, ss.getId());
}

function addGradientToRange(myRange){
  var rule1GradientRule = Sheets.newGradientRule();
  var ptMax = Sheets.newInterpolationPoint();
  var ptMid = Sheets.newInterpolationPoint();
  var ptMin = Sheets.newInterpolationPoint();  
  var colorGreen = Sheets.newColor();
  colorGreen.green = 1;
  var colorRed = Sheets.newColor();
  colorRed.red = 1;
  var colorWhite = Sheets.newColor();  

  ptMax.color = colorGreen;  
  ptMid.color = colorWhite;
  ptMin.color = colorRed;
  ptMax.type = "MAX";//