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?

More than 1 year has passed since last update.

【GAS】開いた時に任意のセルを左上に表示する方法

Last updated at Posted at 2023-12-17

概要

  • スプレッドシートで,開いた時に任意のセルを表示させたい場面がある.
  • GASを利用してsheet.getRange().activate()で表示することはできるが,左上ではなく右下など見づらい所に表示される.
  • 本記事では,任意のセルをページ左上に表示させる方法を紹介する.

サンプルコード

function onOpen() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];

    var cell = "D5"; //左上に表示させたいセル
    
    var lastRows = sheet.getMaxRows();
    var lastClms = sheet.getMaxColumns();
    sheet.getRange(lastRows,lastClms).activate();
    SpreadsheetApp.flush();
    sheet.getRange(cell).activate();
}
  • まず,sheet.getMaxRows()sheet.getMaxColumns()でシート右下のセル位置を取得.
  • その後,その右下のセルをsheet.getRange(lastRows,lastClms).activate()で表示.
    • 一度,右下のセルを表示させることで,表示させたいセルが左上にくるようになる.
  • SpreadsheetApp.flush()で右下のセルを確実に表示させる.
  • 最後に,表示させたいセルをsheet.getRange(cell).activate()で表示させる.
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?