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?
I believe your goal as follows.
For this, how about this answer? In this answer, I would like to propose 2 patterns.
In this pattern, Spreadsheet service is used.
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet(); // Please set the sheet name.
ss.getSheetByName("Sheet1").setRightToLeft(true);
}
rightToLeft
.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));
}
In this pattern, Sheets API is used.
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());
}
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());
}
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.