0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

<コピペで簡単>GAS(Google Apps Script)をつかって、SpreadSheetのグラフを正方形にする

Last updated at Posted at 2025-02-10

はじめに

グラフの形を正方形にする方法です。マウスを使っても正確な正方形は出ないので、GASに頼ることとしました。

体重と目標体重(4).png
こんなふうなグラフが体重と目標体重(3).png
このように完全な正方形になります。
ただし、対象のSpreadsheetのすべてのグラフが正方形になるので注意してください。あと、ダイエットは成功させます。グラフの縦軸ミスってるけど気にしないでください。修正してます。
特定のグラフだけ、みたいなのは作っていないです。

作り方

まずSpreadsheetの、対象のスプレッドシートにアクセスしてください。

次に、”拡張機能”から”Apps Script”を選択してください。

遷移すると思うので、そこにこのコードをペーストしてください。(デフォルトで存在するコードは消して構いません)

function resizeChartToSquare() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var charts = sheet.getCharts();

  if (charts.length === 0) {
    Logger.log("グラフが見つかりません。");
    return;
  }

  charts.forEach(function(chart) {
    var builder = chart.modify();  // グラフの変更用ビルダーを取得
    builder.setOption('width', 1000);  // 幅を1000pxに設定
    builder.setOption('height', 1000); // 高さを1000pxに設定(正方形)

    sheet.updateChart(builder.build()); // 更新
  });

  Logger.log("グラフのサイズを正方形(1000*1000)に変更しました。");
}

ちなみに、正方形以外にもピクセル数を指定することは可能です。

    builder.setOption('width', 1000);  // 幅を1000pxに設定
    builder.setOption('height', 1000); // 高さを1000pxに設定(正方形)

の1000の部分を任意の値に変えるだけです。

”ドライブにプロジェクトを保存”をクリックしたら、”実行”ボタンをクリックしましょう。

すると、Spreadsheetのすべてのグラフが正方形(ここでは1000px*1000px)になっているはずです。

さいごに

いかがでしたか?

もし問題があればAIに聞いてみてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?