Google Sheets: Spreadsheets Multiple Cells Value, Update

Google Sheets: Spreadsheets Multiple Cells Value, Update

Google スプレッドシート: 複数セル値, 更新(個別指定)

Overwrites mulitiple cells of a spreadsheet. The value will be parsed as if user typed text into the Google Sheets UI. For example, if date formated, the value “12-24” will be converted to “12/24/2021”. Or, “’12-31” will be updated as the string “12-31”.

Auto Step icon
Configs for this Auto Step
AuthzConfA
A: Select HTTP_Authz Setting *
StrConfB1
B1: Set ID of Spreadsheet into which Value will be inserted *#{EL}
StrConfB2
B2: Set Name of Sheet into which Value will be inserted#{EL}
StrConfC1
C1: Set Cell1 Coordinate into which Value inserted (eg “A1”) *#{EL}
StrConfD1
D1: Set Value1 to Overwrite#{EL}
StrConfC2
C2: Set Cell2 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD2
D2: Set Value2 to Overwrite#{EL}
StrConfC3
C3: Set Cell3 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD3
D3: Set Value3 to Overwrite#{EL}
StrConfC4
C4: Set Cell4 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD4
D4: Set Value4 to Overwrite#{EL}
StrConfC5
C5: Set Cell5 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD5
D5: Set Value5 to Overwrite#{EL}
StrConfC6
C6: Set Cell6 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD6
D6: Set Value6 to Overwrite#{EL}
StrConfC7
C7: Set Cell7 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD7
D7: Set Value7 to Overwrite#{EL}
StrConfC8
C8: Set Cell8 Coordinate into which Value inserted (eg “A1”)#{EL}
StrConfD8
D8: Set Value8 to Overwrite#{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

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

  • The spreadsheet’s file ID is included in the URL
  • If the sheet name is omitted, it will be inserted into the first non-hidden sheet.
  • Cell coordinates are specified in A1 notation (“A” is the column number of the cell, and “1” is the row number of the cell).
  • If the override value is an empty string, the cell data will be emptied (removed).
  • Multi-line string overwriting is also supported.
  • For Merged Cells, the value is retained only in the top left cell.

Capture

Appendix

See also