Namokonov

ЗАКРЫВАЕМ ДИАПАЗОНЫ ПО ДАТЕ | https://t.me/google_sheets

Aug 6th, 2020
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //друзья, привет, вам нужно заполнить только строки 4,5,6,11
  2. //если будут вопросы, то приходите
  3.  
  4. const dateColumn = 1;
  5. const workSheetName = 'Ежедневный отчет';
  6. const protectRangeName = 'закрытые дни';
  7.  
  8. //кому разрешим редактировать защищенный диапазон
  9. //через запятую перечисляйте емейлы других сотрудников,
  10. //себя можно не добавлять
  11. const editors = [];
  12.  
  13. function closeRows() {
  14.  
  15.   const ss = SpreadsheetApp.getActive();
  16.   const date = new Date((new Date).getTime() - 1000*60*60*24);
  17.   const sheet = ss.getSheetByName(workSheetName);
  18.   delProtection(sheet, protectRangeName);
  19.   createProtection([sheet.getRange(2, 1, findRow(sheet, dateColumn)-1, sheet.getMaxColumns())], [protectRangeName]);
  20.  
  21. }
  22.  
  23. function findRow(sh, column) {  
  24.   const data = sh.getDataRange().getValues();
  25.   const date = new Date((new Date).getTime() - 1000*60*60*24);  
  26.   return Math.max(...(data.map((g, i) => g[column-1] && g[column-1] <= date ? ++i : '')));
  27. }
  28.  
  29.  
  30. function delProtection(sheet, name) {
  31.   var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  32.   for (var i = 0; i < protections.length; i++) {
  33.     var protection = protections[i];
  34.     if (protection.canEdit()) {    
  35.       protection.getDescription() == name ? protection.remove() : '';
  36.     }
  37.   }
  38. }
  39.  
  40. function createProtection(range, names) {
  41.   var me = Session.getEffectiveUser();
  42.  
  43.   range.map((rng, i) => {  
  44.         var protection = rng.protect().setDescription(names[i]);  
  45.         protection.removeEditors(protection.getEditors());
  46.         protection.canDomainEdit() ? protection.setDomainEdit(false) : '';
  47.  
  48.         protection.addEditors([me].concat(editors));
  49. })
  50. }
  51.  
  52. function onOpen(e){
  53.   SpreadsheetApp.getUi()
  54.   .createMenu("🐽 скрипты 🐽")
  55.   .addItem("запустить скрипт закрытия диапазонов вручную", "closeRows")
  56.   .addToUi();
  57. }
  58.  
Add Comment
Please, Sign In to add comment