テーマの取得
特定のスプレッドシートから、適用されているテーマを取得する方法を解説します。
利用する関数
Section titled “利用する関数”Spreadsheet.getSpreadsheetTheme();Spreadsheet クラスに用意されている、getSpreadsheetTheme() を使用します。
返り値は SpreadsheetTheme クラスとして返却されます。テーマが適用されていない場合は null が返されます。
このクラスの getFontFamily() でテーマのフォントファミリーを、getThemeColors() でテーマで使用できる色の種類(ThemeColorType の配列)を取得できます。具体的な色は getConcreteColor() で取得します。
実行にあたってスプレッドシートにアクセスすることになるため、初回実行時に認証が必要になる場合があります。
サンプルコード
Section titled “サンプルコード”/** * 対象スプレッドシートのID * https://docs.google.com/spreadsheets/d/〇〇〇/edit の〇〇〇の部分 */const SPREAD_SHEET_ID = '_______________';
/** * スプレッドシートのテーマを返却します */const getSheetTheme = () => { /** 対象スプレッドシート */ const ss = SpreadsheetApp.openById(SPREAD_SHEET_ID);
/** テーマ情報 */ const theme = ss.getSpreadsheetTheme();
if (!theme) { console.log('テーマが適用されていません'); return null; }
console.log(theme.getFontFamily()); console.log(theme.getThemeColors());
return theme;};実行すると、スプレッドシートのテーマが返却されます。
ログにはテーマのフォントファミリーと、テーマで使用できる色の種類が出力されます。