Microsoft 365 Excel: テーブル行追加
Microsoft 365 Excel: Table Row Create
Excelのテーブルの末尾に1行追加し、その各セルにデータを入力します。(最大15項目まで)
Configs for this Auto Step
- conf_OAuth2
- C1: OAuth2 設定 *
- conf_Url
- C2: 入力先のブックの URL *
- conf_TableName
- C3: 入力先のテーブル名 of テーブルID *#{EL}
- conf_Count
- C4: 追加する列数 (最大 15 列) *
- conf_Column1
- C5_1: 末尾行の 1 列目に追加される値#{EL}
- conf_Column2
- C5_2: 末尾行の 2 列目に追加される値#{EL}
- conf_Column3
- C5_3: 末尾行の 3 列目に追加される値#{EL}
- conf_Column4
- C5_4: 末尾行の 4 列目に追加される値#{EL}
- conf_Column5
- C5_5: 末尾行の 5 列目に追加される値#{EL}
- conf_Column6
- C5_6: 末尾行の 6 列目に追加される値#{EL}
- conf_Column7
- C5_7: 末尾行の 7 列目に追加される値#{EL}
- conf_Column8
- C5_8: 末尾行の 8 列目に追加される値#{EL}
- conf_Column9
- C5_9: 末尾行の 9 列目に追加される値#{EL}
- conf_Column10
- C5_10: 末尾行の 10 列目に追加される値#{EL}
- conf_Column11
- C5_11: 末尾行の 11 列目に追加される値#{EL}
- conf_Column12
- C5_12: 末尾行の 12 列目に追加される値#{EL}
- conf_Column13
- C5_13: 末尾行の 13 列目に追加される値#{EL}
- conf_Column14
- C5_14: 末尾行の 14 列目に追加される値#{EL}
- conf_Column15
- C5_15: 末尾行の 15 列目に追加される値#{EL}
- conf_IndexNum
- C6: 追加したインデックス番号の値を保存するデータ項目 (更新)
Script (click to open)
// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
// - Token Endpoint URL: https://login.microsoftonline.com/common/oauth2/v2.0/token
// - Scope: https://graph.microsoft.com/Files.ReadWrite.All offline_access
// - Consumer Key: (Get by Microsoft Azure Active Directory)
// - Consumer Secret: (Get by Microsoft Azure Active Directory)
const GRAPH_URI = "https://graph.microsoft.com/v1.0/";
const COLUMN_NUM = 15;
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2 = configs.get( "conf_OAuth2" ) + "";
const bookUrl = retrieveBookUrl();
const tabelName = configs.get( "conf_TableName" ) + "";
// const tabelName = encodeURIComponent(configs.get( "conf_TableName" ) + "");
if(tabelName === "" || tabelName === null){
throw "Table Name is empty.";
}
const count = configs.get( "conf_Count" );
if(count > COLUMN_NUM || count < 1) {
throw "Column Count is invalid.";
}
let values = [];
retrieveValues( count, values );
//// == Calculating / 演算 ==
// Access to the API 1st(Get Book Info)
const bookInfo = getFileInfoByUrl( bookUrl, oauth2 );
// Access to the API 2nd(POST)
postData( bookInfo, tabelName, values, oauth2);
}
/**
* config からブックの URL を読み出す、空ならエラー
* @return {String} ブックの URL
*/
function retrieveBookUrl() {
const bookUrlDef = configs.getObject( "conf_Url" );
let bookUrl;
if ( bookUrlDef === null ) {
bookUrl = configs.get( "conf_Url" )
}else{
bookUrl = engine.findData( bookUrlDef );
}
if ( bookUrl === "" || bookUrl === null){
throw "Book URL is empty."
}
return bookUrl;
}
/**
* 指定された範囲を考慮しつつ追加データを準備
* 範囲を優先し、範囲外の指定データ項目は無視する
* @param {Numeric} count 列数
* @param {Array} values 追加データの配列
*/
function retrieveValues( count, values ) {
// for (let i = 0; i < limit; i++) {
for (let i = 0; i < count; i++) {
const columnConfigName = `conf_Column${i+1}`;
const columnValue = configs.get( columnConfigName );
values.push( columnValue );
}
}
/**
* フォルダの URL からファイル情報(ドライブ ID とファイル ID)を取得し、
* オブジェクトで返す(URL が空の場合はエラーとする)
* @param {String} fileUrl フォルダの URL
* @param {String} oauth2 OAuth2 設定
* @return {Object} fileInfo ファイル情報 {driveId, fileId}
*/
function getFileInfoByUrl( fileUrl, oauth2 ) {
let fileInfo;
if ( fileUrl !== "" && fileUrl !== null ) {
// 分割代入
const {
id,
parentReference: {
driveId
}
} = getObjBySharingUrl( fileUrl, oauth2 );
fileInfo = {driveId: `drives/${driveId}`, fileId: id};
}
return fileInfo;
}
/**
* OneDrive のドライブアイテム(ファイル、フォルダ)のメタデータを取得し、JSON オブジェクトを返す
* API の仕様:https://docs.microsoft.com/ja-jp/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online
* @param {String} sharingUrl ファイルの共有 URL
* @param {String} oauth2 OAuth2 設定
* @return {Object} responseObj ドライブアイテムのメタデータの JSON オブジェクト
*/
function getObjBySharingUrl( sharingUrl, oauth2 ) {
if (sharingUrl === "" || sharingUrl === null) {
throw `Sharing URL is empty.`;
}
// encoding sharing URL
const encodedSharingUrl = encodeSharingUrl(sharingUrl);
// API Request
const response = httpClient.begin()
.authSetting( oauth2 )
.get( `${GRAPH_URI}shares/${encodedSharingUrl}/driveItem` );
const responseStr = logAndJudgeError(response, "GET");
return JSON.parse( responseStr );
}
/**
* 共有URLをunpadded base64url 形式にエンコードする
* @param {String} sharingUrl 共有 URL
* @return {String} encodedSharingUrl エンコードされた共有 URL
*/
function encodeSharingUrl( sharingUrl ) {
let encodedSharingUrl = base64.encodeToUrlSafeString( sharingUrl );
while ( encodedSharingUrl.slice(-1) === '=' ) {
encodedSharingUrl = encodedSharingUrl.slice(0,-1);
}
return `u!${encodedSharingUrl}`;
}
/**
* 指定シートの指定行にデータを入力する
* @param {Object} bookInfo
* @param {String} bookInfo.driveId ワークブックのドライブ ID
* @param {String} bookInfo.fileId ワークブックのファイル ID
* @param {String} tableName 挿入先シートの名前
* @param {Array} values 挿入するデータの配列
* @param {String} oauth2 OAuth2 設定
*/
function postData( {driveId, fileId}, tableName, values, oauth2 ){
const postUri = `${GRAPH_URI}${driveId}/items/${fileId}/workbook/tables/${tableName}/rows`;
const requestBody = makeRequestToAdd(values);
//engine.log("json:" + JSON.stringify(requestBody)+"")
const response = httpClient.begin()
.authSetting( oauth2 )
.body( JSON.stringify(requestBody), "application/json" )
.post( postUri );
logAndJudgeError(response, "POST");
const dataId = configs.get( "conf_IndexNum" );
if ( dataId ){
const jsonObj = JSON.parse( response.getResponseAsString() );
const index = jsonObj.index + "";
engine.setDataByNumber( dataId, index + "" );
}
}
/**
* 新しい行に追加するデータを、JSON 形式に変換する
* @param {Array} values データの入った配列
* @return {JSON Object} 変換した JSON オブジェクト
*/
function makeRequestToAdd(values){
let request = {
values : [[]]
};
for(let i = 0; i < values.length; i++){
if(values[i] === "" || values[i] === null){
request.values[0].push(null);
}else{
if(values[i].length > 32767){
throw "Can't set text over 32,767 character.";
}
request.values[0].push(values[i]);
}
}
return request;
}
/**
* ログの出力と、エラー発生時のスローを行う
* @param {HttpResponseWrapper} response リクエストの応答
* @param {String} requestType リクエストをどの形式で行ったか("GET" or "POST" or "PATCH")
* @return {String} responseStr レスポンスの文字列
*/
function logAndJudgeError(response, requestType){
const responseStr = response.getResponseAsString();
//engine.log("response:" + responseStr);
const status = response.getStatusCode();
if(status >= 300){
const accessLog = `---${requestType} request--- ${status}\n${responseStr}\n`;
engine.log(accessLog);
throw `Failed in ${requestType} request. status: ${status}`;
}
return responseStr;
}
Download
- excel-table-row-create.xml
- 2023-07-03 (C) Questetra, Inc. (MIT License)
自由改変可能な JavaScript (ECMAScript) コードです。いかなる保証もありません。
(アドオン自動工程のインストールは Professional editionでのみ可能です)
(アドオン自動工程のインストールは Professional editionでのみ可能です)
Notes
- Microsoft365 系のサービスとの連携設定について
- Microsoft365(Azure Active Directory)側のアプリケーション登録の方法
- Questetra 側の HTTP 認証設定の方法
- “OneDrive へクラウドワークフロー Questetra からファイル出力する方法”
- “2.2: Questetra 側の OAuth 設定”
- ※注意※ Excel Online ファイルが SharePoint Online(SPO)のドキュメントライブラリ上にある場合、指定すべきスコープが異なる
- SPOドキュメントライブラリ上ではない →https://graph.microsoft.com/Files.ReadWrite offline_access
- SPOドキュメントライブラリ上にある →https://graph.microsoft.com/Sites.ReadWrite.All offline_access
- “OneDrive へクラウドワークフロー Questetra からファイル出力する方法”
- Excel側に「テーブル」の設定が必要になります。こちらのページを参考にしてください。
- 「追加する列数」と「末尾行の〇行目に追加される値」の指定が不整合となるケースは、「追加する列数」が優先されます。
例えば、列数の指定が3の場合、4行目以降の追加される値は無視されます。 - シート名にカッコ等の記号が入っている場合にはエラーになることがあります。エラーとなった場合にはシート名の変更を検討してください。
Capture

Appendix
このアドオンXMLは、テーブルを利用しているので、「Microsoft 365 Excel: 行追加」よりも競合に強い作りとなっています。
※「Microsoft 365 Excel: 行追加」の内部処理としては、値または書式設定が割り当たっているセル範囲から末尾行を判定(内部的に usedRange() を利用)し、行追加を行います。 そのため、複数のプロセスから同時に実行された場合、競合により処理が正常に完了しない可能性があります。
