Last active
May 10, 2021 05:02
-
-
Save youknowcast/be248bb1f7a67e8508d19446bff705c8 to your computer and use it in GitHub Desktop.
fivefinger-app 用 GAS コード
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 getUsers() { | |
const appProps = PropertiesService.getDocumentProperties() | |
const usersStr = appProps.getProperty('users') | |
const users = JSON.parse(usersStr) | |
return users | |
} | |
function setUsers(users) { | |
const appProps = PropertiesService.getDocumentProperties() | |
appProps.setProperty('users', JSON.stringify(users)) | |
} | |
function updateResult(value) | |
{ | |
const userProps = PropertiesService.getUserProperties() | |
const userId = userProps.getProperty('id') | |
const users = getUsers() | |
// update users | |
let idx = users.findIndex((u) => u.id === userId) | |
users[idx].value = value | |
setUsers(users) | |
} | |
function refresh() | |
{ | |
showUserNameOnSelectedFinger() | |
} | |
function showUserNameOnSelectedFinger() | |
{ | |
const sheet = SpreadsheetApp.getActiveSheet() | |
// clear all | |
const showUserCell = sheet.getRange('B10:J10') | |
showUserCell.setValue('') | |
const users = getUsers() | |
users.forEach((u) => { | |
let target = 'B10' | |
switch(u.value) { | |
case 1: | |
target = 'B10' | |
break | |
case 2: | |
target = 'D10' | |
break | |
case 3: | |
target = 'F10' | |
break | |
case 4: | |
target = 'H10' | |
break | |
case 5: | |
target = 'J10' | |
break | |
} | |
const targetCell = sheet.getRange(target) | |
let result = targetCell.getValue() | |
result = result + ' ' + u.name | |
targetCell.setValue(result) | |
}) | |
} | |
function clearAll() | |
{ | |
setUsers([]) | |
const sheet = SpreadsheetApp.getActiveSheet() | |
const aveCell = sheet.getRange('E12') | |
aveCell.setValue(0) | |
const showUsersCell = sheet.getRange('B10:J10') | |
showUsersCell.setValue('') | |
const showCountCell = sheet.getRange('C12') | |
showCountCell.setValue(0) | |
} | |
function onClick1(e) { | |
const fiveFingerValue = 1; | |
updateResult(fiveFingerValue) | |
showUserNameOnSelectedFinger() | |
calcAverage() | |
} | |
function onClick2(e) { | |
const fiveFingerValue = 2; | |
updateResult(fiveFingerValue) | |
showUserNameOnSelectedFinger() | |
calcAverage() | |
} | |
function onClick3(e) { | |
const fiveFingerValue = 3 | |
updateResult(fiveFingerValue) | |
showUserNameOnSelectedFinger() | |
calcAverage() | |
} | |
function onClick4(e) { | |
const fiveFingerValue = 4 | |
updateResult(fiveFingerValue) | |
showUserNameOnSelectedFinger() | |
calcAverage() | |
} | |
function onClick5(e) { | |
const fiveFingerValue = 5 | |
updateResult(fiveFingerValue) | |
showUserNameOnSelectedFinger() | |
calcAverage() | |
} | |
function showUserNameInput() | |
{ | |
const ui = SpreadsheetApp.getUi() | |
const res = ui.prompt('お名前'); | |
if (res.getSelectedButton() == ui.Button.CLOSE) { | |
return | |
} | |
// set user name and id | |
const userName = res.getResponseText() | |
const userId = Utilities.getUuid() | |
const userProps = PropertiesService.getUserProperties() | |
userProps.setProperty('name', userName) | |
userProps.setProperty('id', userId) | |
// add user and selected_value | |
let users = getUsers() | |
if (!Array.isArray(users)) { | |
users = [] | |
} | |
user = { | |
id: userId, | |
name: userName, | |
value: 0 | |
} | |
users.push(user) | |
setUsers(users) | |
// add user count | |
length = users.length | |
// show count | |
const sheet = SpreadsheetApp.getActiveSheet() | |
const countCell = sheet.getRange('C12') | |
countCell.setValue(length) | |
} | |
function calcAverage() | |
{ | |
const users = getUsers() | |
const result = users.reduce((prev, current) => prev + current.value, 0) | |
const length = users.length | |
const ave = Math.round(result / length * 100) / 100 | |
const sheet = SpreadsheetApp.getActiveSheet() | |
const aveCell = sheet.getRange('E12') | |
aveCell.setValue(ave) | |
} | |
function debug(value) | |
{ | |
const sheet = SpreadsheetApp.getActiveSheet() | |
const cell = sheet.getRange('B15') | |
cell.setValue(value) | |
} | |
function dump() | |
{ | |
const sheet = SpreadsheetApp.getActiveSheet() | |
const usersCell = sheet.getRange('B16') | |
const userIdCell = sheet.getRange('B17') | |
const userNameCell = sheet.getRange('B18') | |
const users = getUsers() | |
usersCell.setValue(JSON.stringify(users)) | |
const userProps = PropertiesService.getUserProperties() | |
const userId = userProps.getProperty('id') | |
userIdCell.setValue(userId) | |
const userName = userProps.getProperty('name') | |
userNameCell.setValue(userName) | |
} | |
function log() | |
{ | |
const sheet = SpreadsheetApp.getActiveSheet() | |
const date = sheet.getRange('B2').getValue() | |
const count = sheet.getRange('C12').getValue() | |
const ave = sheet.getRange('E12').getValue() | |
const users = getUsers() | |
const persons = users.map(v => v.name).join(',') | |
const ss = SpreadsheetApp.getActiveSpreadsheet(); | |
const logSheet = ss.getSheetByName('log'); | |
const logTargetCols = logSheet.getRange('A:A').getValues() | |
const lastRow = logTargetCols.filter(String).length + 1 | |
logSheet.getRange(`A${lastRow}`).setValue(date) | |
logSheet.getRange(`B${lastRow}`).setValue(count) | |
logSheet.getRange(`C${lastRow}`).setValue(ave) | |
logSheet.getRange(`D${lastRow}`).setValue(persons) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SpreadSheet 上のメソッド binding 一覧
![スクリーンショット 2021-04-18 17 11 16](https://user-images.githubusercontent.com/2378303/115138792-35d39100-a069-11eb-9ecc-f2451cd9d0e9.png)