Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

はじめに

簡単なデータ管理を、スプレッドシート + スクリプトでささっと済ませたい
けど公式ドキュメントを読むのも面倒
そんなときのために、よく使いそうな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);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした