Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const startrow = 3
- function start() {
- const ss = SpreadsheetApp.getActiveSpreadsheet();
- const sheets = ss.getSheets()
- sheets.forEach(sheet => {
- lock(sheet)
- })
- }
- function lock(sheet) {
- const values = sheet.getRange(startrow, 1, sheet.getLastRow() - 2, 2).getValues();
- const now = new Date().getTime()
- let currentDate
- let endrow
- for (let i = 0; i < values.length; i++) {
- let [time, date] = values[i]
- if (time == "") {
- currentDate = date
- continue
- }
- //Pase the time
- time = new Date(time)
- time = Utilities.formatDate(time, Session.getScriptTimeZone(), "hh:mm:ss")
- time = time.split(" ")[0]
- //Combine date and time
- const dateTime = new Date(`${currentDate.toISOString().split("T")[0]} ${time}`)
- //Keep track of endrow
- if (dateTime.getTime() <= now) {
- endrow = startrow + i
- }
- //Check if we have met a datetime that is great then now. Then set the lock to the previous recorded endrow.
- if (dateTime.getTime() >= now && time != "" && endrow) {
- //Remove lock
- sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).forEach(p => {
- if (p.getDescription() == "AUTOLOCK") {
- p.remove()
- }
- })
- //Set new lock
- sheet.getRange(startrow, 1, endrow, sheet.getLastColumn()).protect()
- .setDescription("AUTOLOCK")
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement