I'll help you with a basic outline, some advice, and some resources to help, but don't expect me to write the whole thing.
Step 1 - Create a custom menu option
You'll want to be able to access you script from the spreadsheet. You can accomplish this by creating a custom menu option. Here's an article by google on custom menus.
Although google uses the simple trigger onOpen
, I've found installable triggers to be more reliable.
Step 2 - Get user input
It would be nice to be prompted for the id of the employee and have the script work it's magic. Here is an article by google on dialogs and sidebars that discusses how to get user input for a script.
Step 3 - Read and write data from the spreadsheet
You can access spreadsheeet data with the SpreadsheetApp
. When calling your script from a custom menu, you can use SpreadsheetApp.getActiveSpreadsheet
; however, if you want to test your code in the script editor, there is no "active spreadsheet", so use SpreadsheetApp.openById
instead.
With access to the spreadsheet, you can call:
spreadsheet.getSheetByName("name").getRange(1, 1, 2, 2).getValues();
spreadsheet.getSheetByName("name").getRange(1, 1, 2, 2).setValues([[1, 2], [3, 4]]);
Note that getValues
and setValues
work with 2-dimensional arrays
WARNING - As usual, I/O takes a lot of processing time so avoid superfluously calling getRange().XetValues
, this google article about appscript best practices goes into more detail. Also, if you have a LOT of attendance sheets, then you may want to consider condensing them into one.
Step 4 - Get the appropriate sheets
You'll need someway to distinguish which sheets in the spreadsheet are attendance sheets:
function isAttendanceSheet(sheet) {
return /Magical regex/.test(sheet.getName);
}
var sheets = spreadsheet.getSheets().filter(isAttendanceSheet);
Come up with the magical regex that works for you, and this will filter them.
Hope this helps and good luck!
SpreadsheetApp
service. See this for more details: developers.google.com/apps-script/guides/services/authorization – Douglas Gaskell