
Google スプレッドシート: ファイル作成
この工程は、Google ドライブ の指定フォルダ内に新しいスプレッドシートを作成します。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- Conf_User
- C1: Google ドライブ に接続するユーザ(要アプリ管理権限) *
- Conf_ParentFolderId
- C2: 親フォルダの ID (指定がない場合、マイドライブのルートフォルダ)
- Conf_FileName
- C3: ファイル名 *#{EL}
- Conf_SheetTitle
- C4: 最初のシートのタイトル *#{EL}
- Conf_FileIdItem
- C5: スプレッドシートのファイル ID を保存する文字型データ項目
- Conf_WebViewUrlItem
- C6: スプレッドシートの表示 URL を保存する文字型データ項目
Capture

Notes
- C1 のユーザは、[アカウント設定]>[Google 連携]にて、Google ドライブと連携済みである必要があります
- ワークフロー基盤にて、Google 連携設定([システム設定]>[Google 連携])が必要です(要[システム管理権限])
See also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- google-sheets-file-create.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
main();
function main() {
const parentFolderIdDef = configs.getObject("Conf_ParentFolderId");
let parentFolderId = configs.get("Conf_ParentFolderId");
if (parentFolderIdDef !== null) {
parentFolderId = engine.findData(parentFolderIdDef);
}
const fileName = configs.get("Conf_FileName");
if (fileName === "" || fileName === null) {
throw "File Name is blank";
}
const sheetTitle = configs.get("Conf_SheetTitle");
if (sheetTitle === "" || sheetTitle === null) {
throw "Sheet Title is blank";
}
if (sheetTitle.length > 100) {
throw "Sheet Title should be 100 characters or less";
}
const quser = configs.getObject('Conf_User');
if (quser === null) {
throw "User not found";
}
engine.log(`User Name: ${quser.getName()}`);
const { id, webViewLink } = createFile(quser, parentFolderId, fileName);
updateSheetTitle(quser, id, sheetTitle);
setData("Conf_FileIdItem", id);
setData("Conf_WebViewUrlItem", webViewLink);
}
/**
* データ項目に出力する
* @param {ProcessDataDefinitionView} configName データ項目の ProcessDataDefinitionView
* @param {String} data 出力する文字列
*/
function setData(configName, data) {
const def = configs.getObject(configName);
if (def === null) {
return;
}
engine.setData(def, data);
}
/**
* google スプレッドシートを親フォルダに作成する
* @param {QuserView} quser Google ドライブ に接続するユーザ
* @param {String} parentFolderId 親フォルダ
* @param {String} fileName ファイル名
* @return {Object} createFile スプレッドシートのファイル IDと表示 URLを格納した JSON オブジェクト
* プロパティ: {String} id スプレッドシートのファイル ID
* {String} webViewLink スプレッドシートの表示 URL
*/
function createFile(quser, parentFolderId, fileName) {
let jsonReq = {};
jsonReq["mimeType"] = "application/vnd.google-apps.spreadsheet";
if (parentFolderId !== "" && parentFolderId !== null) {
jsonReq["parents"] = [String(parentFolderId)];
}
jsonReq["name"] = String(fileName);
const url = 'https://www.googleapis.com/drive/v3/files/';
const response = httpClient.begin()
.googleOAuth2(quser, "Drive")
.queryParam("fields", "id, webViewLink")
.queryParam("supportsAllDrives", "true")
.body(JSON.stringify(jsonReq), "application/json")
.post(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
engine.log(`status of file create: ${status}`);
if (status !== 200) {
const error = `Failed to create \n status: ${status}`;
engine.log(responseTxt);
throw error;
}
const jsonRes = JSON.parse(responseTxt);
const res = {
id: jsonRes["id"],
webViewLink: jsonRes["webViewLink"]
}
return res;
}
/**
* google スプレッドシートのシート名を更新する
* @param {QuserView} quser Google ドライブ に接続するユーザ
* @param {String} fileId ファイルID
* @param {String} sheetTitle シート名
*/
function updateSheetTitle(quser, fileId, sheetTitle) {
let jsonReq = {
requests: []
};
jsonReq.requests[0] = {
updateSheetProperties: {
fields: 'title',
properties: {
sheetId: 0,
title: sheetTitle
}
}
};
const url = `https://sheets.googleapis.com/v4/spreadsheets/${fileId}:batchUpdate`;
const response = httpClient.begin()
.googleOAuth2(quser, "Drive")
.body(JSON.stringify(jsonReq), "application/json")
.post(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
engine.log(`status of sheet title update: ${status}`);
if (status !== 200) {
const error = `Failed to update sheet title \n status: ${status}`;
engine.log(responseTxt);
throw error;
}
}