Last active
October 5, 2017 04:47
-
-
Save shingorow/e9e09eb8bf38c06fceae1e11aa285aca to your computer and use it in GitHub Desktop.
Google Forms で申し込み数制限のある選択リストを作成するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* スプレッドシートから選択項目一覧を取得する | |
* | |
* @param {string} ssId スプレッドシートの ID | |
* @param {string} sheetName リストを記述してあるシート名 | |
* | |
* @return {Object} 選択項目名のリスト | |
**/ | |
var getMultichoiceItemList = function(ssId, sheetName) { | |
var ss = SpreadsheetApp.openById(ssId); | |
var sheet = ss.getSheetByName(sheetName); | |
var range = sheet.getDataRange(); | |
var values = range.getValues(); | |
return values.filter(function(row) { | |
return row[3] === true; | |
}); | |
} | |
/** | |
* フォームの選択肢を作成し、セットする | |
* | |
* @param {string} formId フォームの ID | |
* @param {string} itemId 操作するアイテムの ID | |
* | |
**/ | |
var setMultichoiceItem = function(formId, itemId, items) { | |
var itemValues = items.map(function(item) { | |
return item[0]; | |
}); | |
var form = FormApp.openById(formId); | |
var item = form.getItemById(itemId); | |
if (itemValues.length === 0) itemValues = ['現在募集していません']; | |
item.asMultipleChoiceItem().setChoiceValues(itemValues); | |
} | |
function createMultichoiceItem() { | |
var properties = PropertiesService.getScriptProperties(); | |
// Script のプロパティにそれぞれの値を持っておく | |
var spreadsheetId = properties.getProperty('SPREADSHEET_ID'); | |
var sheetName = properties.getProperty('SHEET_NAME'); | |
var formId = properties.getProperty('FORM_ID'); | |
var itemId = properties.getProperty('ITEM_ID'); | |
var multichoiceItems = getMultichoiceItemList(spreadsheetId, sheetName); | |
setMultichoiceItem(formId, itemId, multichoiceItems); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getItems() { | |
var formId = PropertiesService.getScriptProperties().getProperty('FORM_ID'); | |
var form = FormApp.openById(formId); | |
var items = form.getItems(); | |
items.forEach(function(item) { | |
Logger.log('title: ' + item.getTitle() + ', ' + 'ID: ' + item.getId()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment