Created
April 28, 2021 00:39
-
-
Save tamanobi/e78723b1b921d036dec1291ffde46d12 to your computer and use it in GitHub Desktop.
Google フォームの回答時に回答コピーと、別のフォームURLを送信する機能
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 getHeaders(sheet) { | |
var last_col = sheet.getLastColumn(); | |
var range = sheet.getRange(1, 1, 1, last_col); | |
var headers = range.getValues(); | |
return headers[0]; | |
} | |
function getColumnNumByHeader(sheet, header) { | |
var n = getHeaders(sheet).indexOf(header); | |
if (n < 0) { | |
throw Error("ヘッダーが見つからなかった"); | |
} | |
return n + 1; | |
} | |
function getRandomInt(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(min + (max - min) * Math.random()); | |
} | |
function getProps(value, key) { | |
return value[key][0]; | |
} | |
function keyValue(key, value, assistText) { | |
var message = "【" + key + "】\n" + value + "\n"; | |
if (assistText) { | |
return message + "\n\n" + assistText +"\n"; | |
} | |
return message; | |
} | |
function zeroPadding(n, l){ | |
return ( Array(n).join('0') + n ).slice( -l ); | |
} | |
function onFormSubmit(e) { | |
console.log(Object.keys(e.namedValues)); | |
var namedValues = e.namedValues; | |
var keys = Object.keys(namedValues); | |
var email = namedValues['メールアドレス'][0]; | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getActiveSheet(); | |
var answer = []; | |
getHeaders(sheet).forEach(function(v, i) { | |
if(keys.indexOf(v) >= 0) { | |
answer.push({key: v, value: getProps(namedValues, v), assistText: ""}); | |
} | |
}); | |
console.log(answer); | |
var answerWithoutEmpty = answer.filter(function(v) {return v.value !== ""}); | |
console.log(answerWithoutEmpty); | |
var confirm = answerWithoutEmpty.map(function(v) {return keyValue(v.key, v.value, v.assistText)}); | |
var mailSetting = spreadsheet.getSheetByName("メール設定"); | |
var subject = mailSetting.getRange("B1").getValue(); | |
var greeting = mailSetting.getRange("B2").getValue(); | |
var formUrl = mailSetting.getRange("B3").getValue(); | |
greetingAssignedVars = greeting.replace(/{{formURL}}/g, formUrl); | |
var body = greetingAssignedVars + '\n' + confirm.join("\n"); | |
if (body.match(/({{)|(}})/)) { | |
console.error("変数プレースホルダが置換されていません", body); | |
return; | |
} | |
MailApp.sendEmail({ | |
to: email, | |
subject: subject, | |
body: body | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment