LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Monday, March 19, 2018

SpreadsheetのQUERY関数でシートを結合する


QUERY関数で2つのシートを結合する(範囲固定)


=QUERY({'対象シート'!範囲;'対象シート'!範囲})


=QUERY({'シート1'!A1:A4;'シート2'!A2:A4})


1. シート1にこういうデータがあり


2. シート2にこういうデータがあり


3. 上記2つのシートのデータをシート3で結合してみる
=QUERY({'シート1'!A1:A4;'シート2'!A2:A4})



QUERY関数で複数列のデータを一列にまとめる


=QUERY({
QUERY(A2:C,"select A,B");
QUERY(A2:C,"select A,C")},
"where Col1 is not null order by Col1")




異なるシートのデータを読み込んで結合する
(QUERY関数、IMPORTRANGE関数)

(スプレッドシートのURLとシート名はご自身のものに置き換えてください)

=query({query(importrange("https://docs.google.com/spreadsheets/d/1i2rtZUstp40O_FjxX3H9X2_hr8WnpyFnKgN0VP0uTzk/edit#gid=0", "シート1!A1:C"),"select Col1"), 

query(importrange("https://docs.google.com/spreadsheets/d/1i2rtZUstp40O_FjxX3H9X2_hr8WnpyFnKgN0VP0uTzk/edit#gid=0", "シート1!A1:C"),"select Col2")})





一列にまとめる

=query({
query(importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=0", "シート1!A2:C"), "select Col1,Col2");
query(importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=0", "シート1!A2:C"), "select Col1,Col3")
},"where Col1 is not null order by Col1")


異なるスプレッドシートのデータを1つのシートにまとめる


=query({
query(importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=0", "シート1!A2:C"));
query(importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=523722773", "シート4!A2:C"))
},"where Col1 is not null order by Col1")


このシートのA2:Cの範囲
importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=0", "シート1!A2:C")

このシートのA2:Cの範囲
https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=523722773", "シート4!A2:C")


上記2つのシートで指定した範囲を別のシートで結合する
=query({
query(importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=0", "シート1!A2:C"));
query(importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=523722773", "シート4!A2:C"))
},"where Col1 is not null order by Col1")


補足
#REF! が出る場合
query関数でまとめる前に importrange関数 だけで読み込んで
「アクセスを許可」してみるとうまくいくかも


こんな感じで試してみる
=importrange("https://docs.google.com/spreadsheets/d/1-U1VYQUofqQO9dY0WyHls4KwuZxJUpu3iw74eeQCTU0/edit#gid=0", "シート1!A2:C")


Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...