Google スプレッドシート: 複数セル値, 更新(個別指定)
Google Sheets: Spreadsheets Multiple Cells Value, Update
特定セル(複数)を上書きします。値は、GoogleスプレッドシートのUIを使用して入力した場合と同じように解析されます。たとえば日付フォーマットが指定されている場合、”12-24″ という値は “12/24/2021” に変換されます。あるいは “’12-31” は “12-31” という文字列として更新されます。
Configs for this Auto Step
- AuthzConfA
- A: HTTP認証設定を選択してください *
- StrConfB1
- B1: 値挿入先のドキュメントID(Spreadsheet ID)をセットしてください *#{EL}
- StrConfB2
- B2: 値挿入先のシート名をセットしてください#{EL}
- StrConfC1
- C1: 値挿入先のセル1座標をセットしてください (例 “A1”) *#{EL}
- StrConfD1
- D1: 上書き値1をセットしてください#{EL}
- StrConfC2
- C2: 値挿入先のセル2座標をセットしてください (例 “A1”)#{EL}
- StrConfD2
- D2: 上書き値2をセットしてください#{EL}
- StrConfC3
- C3: 値挿入先のセル3座標をセットしてください (例 “A1”)#{EL}
- StrConfD3
- D3: 上書き値3をセットしてください#{EL}
- StrConfC4
- C4: 値挿入先のセル4座標をセットしてください (例 “A1”)#{EL}
- StrConfD4
- D4: 上書き値4をセットしてください#{EL}
- StrConfC5
- C5: 値挿入先のセル5座標をセットしてください (例 “A1”)#{EL}
- StrConfD5
- D5: 上書き値5をセットしてください#{EL}
- StrConfC6
- C6: 値挿入先のセル6座標をセットしてください (例 “A1”)#{EL}
- StrConfD6
- D6: 上書き値6をセットしてください#{EL}
- StrConfC7
- C7: 値挿入先のセル7座標をセットしてください (例 “A1”)#{EL}
- StrConfD7
- D7: 上書き値7をセットしてください#{EL}
- StrConfC8
- C8: 値挿入先のセル8座標をセットしてください (例 “A1”)#{EL}
- StrConfD8
- D8: 上書き値8をセットしてください#{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" ) + "";
//// == 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/update
//8セル分をループ
for (let i = 1; i <= 8; i++ ) {
const strCellId = configs.get( "StrConfC" + i ) + "";
const strValue = configs.get( "StrConfD" + i ) + "";
if (strCellId) {
// request1, prepare
let request1Obj = {};
request1Obj.values = [];
request1Obj.values[0] = [];
request1Obj.values[0][0] = strValue;
let putUri1 = "https://sheets.googleapis.com/v4/spreadsheets/" +
strFileId + "/values/" + strSheetName + "!" + strCellId;
let request1 = httpClient.begin(); // HttpRequestWrapper
request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
request1 = request1.queryParam( "valueInputOption", "USER_ENTERED" );
request1 = request1.body( JSON.stringify( request1Obj ), "application/json" );
// request1, try
const response1 = request1.put( putUri1 ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + putUri1 );
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" );
}
// response1, parse
/* engine.log( response1Body ); // debug
{
"spreadsheetId": "1SK4mXXXXXq7ZriYYYYYOJHmIZZZZZAtjynWWWWW-qlE",
"updatedRange": "Sheet1!C3",
"updatedRows": 1,
"updatedColumns": 1,
"updatedCells": 1
}
*/
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()" /////////////////////////////////////////////////////////////////
/*
NOTES
- If the sheet name is omitted, Value will be inserted into the first visible sheet.
- Cell Coordinates in A1 notation (where "A" is the column and "1" is the row of the cell)
- If set the value to an empty string, a cell will be an empty value (deleted).
- It also supports overwriting with multi-line strings.
- For merged cells, the value can only be stored in the upper left cell.
NOTES-ja
- シート名を省略した場合、「非表示でない先頭シート」に挿入されます。
- セル座標はA1記法で指定します("A" はセルの列番号、"1" はセルの行番号です)
- 上書き値が空文字列の場合、セルデータは空になります。(削除されます)
- 複数行文字列での上書きにも対応しています。
- 結合セル(Merged Cell)の場合、値は「左上のセル」にのみ保持されます。
*/
/*
APPENDIX
- OAuth Setting sample [HTTP Authorization Setting]
- "Authorization Endpoint URL"
- https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
- "Token Endpoint URL"
- https://accounts.google.com/o/oauth2/token
- to get your "ClientId" and "Secret"
- Access to https://console.developers.google.com/apis/credentials
- Authorization Scopes (one of the following)
- https://www.googleapis.com/auth/drive
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/spreadsheets
- About the levels of access
- https://developers.google.com/identity/protocols/oauth2/scopes#sheets
APPENDIX-ja
- OAuth 設定例([HTTP認証設定]の例)
- "Authorization Endpoint URL"
- https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
- "Token Endpoint URL"
- https://accounts.google.com/o/oauth2/token
- なお "ClientId" と "Secret" は開発者コンソールにアクセスして取得します。
- Access to https://console.developers.google.com/apis/credentials
- 認可スコープ(以下のいずれか)
- https://www.googleapis.com/auth/drive
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/spreadsheets
- それぞれのアクセスレベルについて
- https://developers.google.com/identity/protocols/oauth2/scopes#sheets
*/
Download
- Google-Sheets-Multiple-Cells-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″ はセルの行番号です)
- 上書き値が空文字列の場合、セルデータは空になります。(削除されます)
- 複数行文字列での上書きにも対応しています。
- 結合セル(Merged Cell)の場合、値は「左上のセル」にのみ保持されます。
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
