Namokonov

searchFilesAndAdd

May 5th, 2021 (edited)
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Наш канал про Таблицы и скрипты в Telegram: https://t.me/google_sheets
  3. Оглавление канала (конечно, в Таблице): https://docs.google.com/spreadsheets/d/1u0aRlwb0rQHbjE9avbgCqIJSQ3EvPfTxT-h54jTQh8I/edit#gid=0&range=A1:B1
  4. Чат: https://t.me/google_spreadsheets_chat
  5. Донаты: https://t.me/google_spreadsheets_chat/160663
  6. Написать: https://t.me/namokonov
  7. */
  8.  
  9. const shName = '⚡️ ПРО НАС';
  10.  
  11. function onOpen(e) {
  12.   SpreadsheetApp.getUi()
  13.     .createMenu("Скрипты ↓↓")
  14.     .addItem("найти Таблицы", "searchFiles")
  15.     .addItem('добавить лист в Таблицы', 'addShToFiles')
  16.     .addToUi();
  17. }
  18.  
  19. function searchFiles() {
  20.   var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  21.   var array = []
  22.   var t = 0
  23.   var max = 5000
  24.   var query = q();
  25.   var files = DriveApp.searchFiles(q())
  26.  
  27.   while (files.hasNext() && t < max) {
  28.     t++
  29.     var file = files.next();
  30.     try {
  31.       array = array.concat([[file.getName(), file.getUrl(), file.getSharingAccess(), file.getDateCreated(), file.getOwner().getEmail()]]);
  32.     } catch (err) { }
  33.   }
  34.   var r = ss.getRange(4, 1, array.length, array[0].length)
  35.   r.clearContent()
  36.   r.setValues(array)
  37. }
  38.  
  39. function q() {
  40.   var userEmail = Session.getActiveUser().getEmail()
  41.  
  42.   return `'${userEmail}' in owners and mimeType = 'application/vnd.google-apps.spreadsheet' and (visibility = 'anyoneWithLink' or visibility = 'anyoneCanFind')`;
  43.  
  44. }
  45.  
  46. function addShToFiles() {
  47.   const ss = SpreadsheetApp.getActive();
  48.   const sh = ss.getActiveSheet()
  49.   const template = ss.getSheetByName(shName);
  50.   const range = sh.getRange("b1:b500")
  51.   const colours = range.getBackgrounds();
  52.  
  53.   range.getValues()
  54.     .forEach((url, i) => {
  55.       url = url[0];
  56.       if (url && colours[i][0] != 'red') {
  57.         const shTo = SpreadsheetApp.openByUrl(url);
  58.         const shToCheck = shTo.getSheetByName(shName);
  59.         if (!shToCheck) {
  60.           template.copyTo(shTo).setName(shName);
  61.           Logger.log('OK ' + shTo.getName())
  62.         }
  63.         sh.getRange("i" + (i + 1)).setBackground('red');
  64.         SpreadsheetApp.flush();
  65.       }
  66.     })
  67. }
  68.  
Add Comment
Please, Sign In to add comment