1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GASを使ってgooglespreadsheetに自動的に目次を生成する

Last updated at Posted at 2021-08-03

google documentがあまりにショボいもので、仕方なくgoogle spreadsheetで仕様書を書かざるを得ないってのは、よくある苦痛だと思う。
そしてspreadはシートが増えるとやってられないので、GASで簡単にURL付きの目次を作れるようにしてみた。

  • sheetに"contents"と名前のついたsheetを作ると、"B3"から1行おきにURLを生成してくれる。
  • myfunctionに入れておいて、sheetが追加されたり更新されたりのトリガで起動すると、自動的に目次が更新される。

sortするとか、もうちょっと目次らしくするとかもできるが、基本関数としてはこれでいいかなと。

myfunction()
  const ss = SpreadsheetApp.getActiveSpreadsheet(); // ともかくspreadsheetを取得する。
  const ssId = ss.getId();
  const contSheet = ss.getSheetByName("contents");
  const sheets = ss.getSheets();

  let sRow=3;
  let sCol=2;
  sheets.forEach(item=>{
    let sheetId = item.getSheetId();
    let sheetName = item.getSheetName();

    let url = "https://docs.google.com/spreadsheets/d/" + ssId + "/edit#gid=" + sheetId;
    let link = [ '=HYPERLINK("' + url + '","' + sheetName + '")' ];

    contSheet.getRange(sRow,sCol,1,1).setValue( link );
    sRow=sRow+2;
  });
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?