Google スプレッドシート: 複数セル値, 一括更新(範囲指定)
Google Sheets: Spreadsheets Cells Value, Bulk Update (range)
シート内の指定された範囲のセル(テーブル型の表を想定)のデータを一括更新します。(改行を含む値は指定できません)
Configs for this Auto Step
- AuthzConfA
- A: HTTP認証設定を選択してください *
- StrConfB1
- B1: 値挿入先のドキュメントID(Spreadsheet ID)をセットしてください *#{EL}
- StrConfB2
- B2: 値挿入先のシート名をセットしてください#{EL}
- StrConfC1
- C1: 対象範囲のセルfrom *#{EL}
- StrConfC2
- C2: 対象範囲のセルto *#{EL}
- StrConfC3
- C3: セルを更新するための設定TSV#{EL}
Script (click to open)
// GraalJS Script (engine type: 3)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting = configs.get( "AuthzConfA" ); // required
engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strFileId = configs.get( "StrConfB1" ) + ""; // required
const strSheetName = configs.get( "StrConfB2" ) + "";
const cell1 = configs.get( "StrConfC1" );
const cell2 = configs.get( "StrConfC2" );
const tsv = configs.get( "StrConfC3" );
//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)
//// == Calculating / 演算 ==
//// Overwrite the value in the cell
/// Sets values in a range of a spreadsheet.
/// (Google Sheets > API v4)
/// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate?hl=ja
// request1, prepare
let request1Obj = {};
request1Obj.valueInputOption = "USER_ENTERED";
request1Obj.data = [];
request1Obj.data[0] = {};
request1Obj.data[0].range = "'" + strSheetName + "'!" + cell1 + ":" + cell2;
request1Obj.data[0].majorDimension = "COLUMNS";
request1Obj.data[0].values = [];
/*
//転置してない場合
const arrLine = tsv.split(/\r\n|\n/);
engine.log("arrLine:"+arrLine.length)
for ( let i = 0; i < arrLine.length; i++ ) {
engine.log("i:"+i)
let array = [];
const arrCell = arrLine[i].split("\t");
engine.log("arrCell:"+arrCell.length)
for ( let j = 0; j < arrCell.length; j++ ) {
engine.log("j:"+j)
engine.log("arrCell[j]:"+arrCell[j])
array.push(arrCell[j]);
}
request1Obj.data[0].values[i] = array;
}
*/
//TSVを転置しつつ二次元配列に展開
const arrLine = tsv.split(/\r\n|\n/);
// engine.log("arrLine:"+arrLine.length)
for ( let i = 0; i < arrLine.length; i++ ) {
// engine.log("i:"+i)
const arrCell = arrLine[i].split("\t");
// engine.log("arrCell:"+arrCell.length)
for ( let j = 0; j < arrCell.length; j++ ) {
// engine.log("j:"+j)
// engine.log("arrCell[j]:"+arrCell[j])
if (i == 0) {
let array = [];
request1Obj.data[0].values[j] = array;
request1Obj.data[0].values[j][i] = arrCell[j]
} else {
request1Obj.data[0].values[j][i] = arrCell[j]
}
}
}
engine.log("JSON.stringify( request1Obj ):" + JSON.stringify( request1Obj ) + "");
let postUri1 = "https://sheets.googleapis.com/v4/spreadsheets/" +
strFileId + "/values:batchUpdate";
let request1 = httpClient.begin(); // HttpRequestWrapper
request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
request1 = request1.body( JSON.stringify( request1Obj ), "application/json" );
// request1, try
const response1 = request1.post( postUri1 ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + postUri1 );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
response1Code + "\n" + response1Body + "\n" );
}
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask ApiRequest1 updateRange: " + response1Obj.updatedRange );
//// == Data Updating / ワークフローデータへの代入 ==
// (No Output except Console Log and response0Obj.values[0][0] )
} //////// END "main()" /////////////////////////////////////////////////////////////////
Download
- Google-Sheets-Cells-Bulk-Update.xml
- 2024-08-01 (C) Questetra, Inc. (MIT License)
自由改変可能な JavaScript (ECMAScript) コードです。いかなる保証もありません。
(アドオン自動工程のインストールは Professional editionでのみ可能です)
(アドオン自動工程のインストールは Professional editionでのみ可能です)
Notes
- スプレッドシートの ファイルID は、URL に含まれています
- https://docs.google.com/spreadsheets/d/SPREADSHEETID/edit#gid=0
- シート名を省略した場合、「非表示でない先頭シート」に挿入されます。
- セル座標はA1記法で指定します(”A” はセルの列番号、”1″ はセルの行番号です)
- 上書き値が空文字列の場合、セルデータは空になります。(削除されます)
Capture

Appendix
- “HTTP認証”(OAuth2)の設定例
- Authorization Endpoint URL:
- Token Endpoint URL:
- Scope:
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
