25
31

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 5 years have passed since last update.

Googleスプレッドシート メソッドまとめ

Last updated at Posted at 2016-01-24

はじめに

簡単なデータ管理を、スプレッドシート + スクリプトでささっと済ませたい
けど公式ドキュメントを読むのも面倒
そんなときのために、よく使いそうなAPIをまとめておきました

※ドキュメントはこちら
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

基本的な操作

Cmd + Rで実行
Cmd + Enterでログを確認
Logger.log(log)でログを格納

オブジェクトの作成

sample.js
// オブジェクトの作成とアクセス
var spreadSheet  = SpreadsheetApp.create("Finances");
var spreadSheet2 = spreadSheet.copy("Copy of " + spreadSheet.getName());
// 2つめに差し込み
var sheet        = spreadSheet.insertSheet('piyo', 1)
// テンプレートから作成
var templateSheet = ss.getSheetByName('hoge');
var sheet        = spreadSheet.insertSheet('hoge2', 1, {template: templateSheet})

各オブジェクトへのアクセス

sample2.js
var spreadSheet  = SpreadsheetApp.openById(spreadSheet.getId('hogehoge'));
var sheet        = spreadSheet.getSheets()[0];
var sheet        = spreadSheet.getSheetByName(name);

var cell         = sheet.getRange('A1');
var range        = sheet.getRange('A1:A3'); 
var cell         = sheet.getRange(1,1); // range object
var fullRange    = sheet.getDataRange();  //データが入ってるレンジ全部

プロパティの取得

sample3.js
var numSheets    = spreadSheet.getNumSheets()
var sheetName    = sheet.getIndex()
var sheetName    = sheet.getName()
var sheetParent  = sheet.getParent()
var sheetVlalues = sheet.getSheetValues(1,1,3,3) // List[value]
var lastColumn   = sheet.getLastColumn()
var lastRow      = sheet.getLastRow()
var rangeValue   = range.getValues("B1:B3")      // List[value]
var rangeFolmula = range.getFormulas("A1:B3")

データの変更

sample4.js
sheet.insertColumnBefore(2);
sheet.insertColumnBefore(2);
sheet.deleteColumns(2, 2); //Columns & Rows start at "1"
sheet.sort(1);
sheet.clear();
cell.setValue("=A1")
range.setvalue([[1],[2],[3]])
range.setFontColors([["red"], ["black"], ["blue"]]);
range.clear()

データバリデーション

validation.js
//いずれかの値をとる
var cell = sheet.getRange('A1:A3');
var rule = SpreadsheetApp.newDataValidation()
                         .requireValueInList(['Yes', 'No'])
                         .build();
cell.setDataValidation(rule);
//ある文字を含まない
var cell = sheet.getRange('A1');
var rule = SpreadsheetApp.newDataValidation()
                         .requireTextDoesNotContain('@')
                         .build();
cell.setDataValidation(rule);
//他のセルの値のどれか
var cell = sheet.getRange('A1');
var range = SpreadsheetApp.getActive().getRange('B1:B10');
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();
cell.setDataValidation(rule);
25
31
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
25
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?