Search code examples
google-apps-scriptgoogle-sheetsright-to-left

How can I change sheet direction from apps script


I'm creating a new sheet from apps script and I want that sheet's direction to be right-to-left (the whole sheet and not the cells themself). is there any way to do it from apps script code?


Solution

  • I believe your goal as follows.

    • You want to change the sheet direction to "right-to-left" using Google Apps Script.

    For this, how about this answer? In this answer, I would like to propose 2 patterns.

    Pattern 1:

    In this pattern, Spreadsheet service is used.

    Sample script:

    function myFunction() {
      const ss = SpreadsheetApp.getActiveSpreadsheet();  // Please set the sheet name.
      ss.getSheetByName("Sheet1").setRightToLeft(true);
    }
    
    • In this case, the sheet direction of "Sheet1" in the active Spreadsheet is changed to rightToLeft.
    • When setRightToLeft(false) is used, "right-to-left" is changed to "left-to-right".
    • If you want to change it for all sheets in the active Spreadsheet, you can also use the following script.

      function myFunction() {
        const ss = SpreadsheetApp.getActiveSpreadsheet();
        ss.getSheets().forEach(s => s.setRightToLeft(true));
      }
      

    Pattern 2:

    In this pattern, Sheets API is used.

    Sample script:

    Before you use this script, please enable Sheets API at Advanced Google services.

    function myFunction() {
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const sheet = ss.getSheetByName("Sheet1");  // Please set the sheet name.
      const resource = {requests: [{
        updateSheetProperties: {
          properties: {sheetId: sheet.getSheetId(), rightToLeft: true},
          fields: "rightToLeft"
        }
      }]};
      Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
    }
    
    • In this case, the sheet direction of "Sheet1" in the active Spreadsheet is changed to rightToLeft.
    • If you want to change it for all sheets in the active Spreadsheet, you can also use the following script.

      function myFunction() {
        const ss = SpreadsheetApp.getActiveSpreadsheet();
        const reqs = ss.getSheets().map(s => ({
          updateSheetProperties: {
            properties: {sheetId: s.getSheetId(), rightToLeft: true},
            fields: "rightToLeft"
          }
        }));
        const resource = {requests: [reqs]};
        Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
      }
      

    Note:

    • When you want to check whether the sheet direction is "right-to-left", you can check it with isRightToLeft(). Ref For example, when you want to check this for "Sheet1", you can use the following script. When true is returned, the sheet direction is "right-to-left".

      const check = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").isRightToLeft();
      console.log(check)
      
    • When you want to change this for a lot of sheets in a Google Spreadsheet, when the Sheets API is used, the process cost might be able to be reduced.

    References: