Last active
April 28, 2021 00:30
-
-
Save tamanobi/eafd5cf3b8a384cfaa6d393b1484f022 to your computer and use it in GitHub Desktop.
Google フォームに連番紹介コードを付与できる
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 generateCode() { | |
var CHARSET = "34679abcdefghijkmnpqrstuvwxyzACEFGHJKMLNPRTUVWXY"; | |
var code = ""; | |
for(var i = 0; i < 6; i++) { | |
code +=CHARSET[getRandomInt(0, CHARSET.length)]; | |
} | |
return code; | |
} | |
function generateCode2() { | |
CURRENT_ROW_NUMBER = 25; // スプレッドシートに何件回答がきているか調査してその数字を入れてください | |
INITIAL_NUMBER = 300; | |
return '=TEXT(ROW() + ' + INITIAL_NUMBER + ' - ' + (CURRENT_ROW_NUMBER + 1) + ', "0000")'; | |
} | |
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 code = generateCode2(); | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getActiveSheet(); | |
var dataRange = sheet.getDataRange(); | |
var lastRow = dataRange.getLastRow(); | |
var codeColumn = dataRange.getCell(lastRow, getColumnNumByHeader(sheet, "発行された紹介コード")); | |
codeColumn.setValue(code); | |
var displayValue = codeColumn.getDisplayValue() | |
console.log('displayValue', displayValue); | |
code = displayValue; | |
var answer = []; | |
getHeaders(sheet).forEach(function(v, i) { | |
if(keys.indexOf(v) >= 0) { | |
answer.push({key: v, value: getProps(namedValues, v), assistText: ""}); | |
} | |
}); | |
answer.push({key: "あなたの紹介コード", value: code, 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