Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide grid lines in google spreadsheet programmatically in a script

Is there a programmatic way to control hide/unhide Gridlines or protected ranges in Google spreadsheet? There is an option in the menu - View > Gridlines or Protected Ranges to do this. But I want to do this in a script.

Thanks.

like image 374
Tim Avatar asked Nov 06 '12 12:11

Tim


3 Answers

Try Sheet.setHiddenGridlines(true). See https://developers.google.com/apps-script/reference/spreadsheet/sheet#setHiddenGridlines(Boolean)

like image 86
pomo Avatar answered Nov 11 '22 13:11

pomo


A little late, but here's a handy snippet to accomplish this by calling the V4 API from within Google Apps Script.

For this to work:

  • You'll need to enable the Sheets v4 API in your Google Cloud Console for the Sheet's bound Google Apps Script project, which you can get to from within the Script editor, from Resources->Cloud Platform project...

  • You'll likely also need to enable the Sheets V4 API Resources->Advanced Google services...

function test() {
  var spreadsheetId = SpreadsheetApp.getActive().getId();
  var sheetId = SpreadsheetApp.getActiveSheet().getSheetId();
  hideGridlines(spreadsheetId, sheetId, false);
}

/**
 * Hide or show gridlines
 *
 * @param {string} spreadsheetId - The spreadsheet to request.
 * @param {number} sheetId - The ID of the sheet.
 * @param {boolean} hideGridlines - True if the grid shouldn't show gridlines in the UI.
 **/
function hideGridlines(spreadsheetId, sheetId, hideGridlines) {
  var resource = {
    "requests": [
      {
        "updateSheetProperties": {
          "fields": "gridProperties(hideGridlines)",    
          "properties": {
            "sheetId": sheetId,
            "gridProperties": {
              "hideGridlines": hideGridlines
            }
          }
        }
      }
    ],
    "includeSpreadsheetInResponse": false,
    "responseIncludeGridData": false,
  }

  Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId)  
}
like image 30
Timothy Johns Avatar answered Nov 11 '22 15:11

Timothy Johns


I use the command:

sheet.getRange(1,1,maxrows,maxcolumns).setBorder(true,true,true,true,false,false,"white");

This change the linecolor for white, which is the same of the background. Then, the gridline is "hidden". This works for me.

like image 1
tbernardes Avatar answered Nov 11 '22 13:11

tbernardes