Advertisement
Namokonov

from sheet to excel

Dec 3rd, 2020 (edited)
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let FOLDER_URL = '';
  2.  
  3. function driveIt(){  
  4.   const ss = SpreadsheetApp.getActive();
  5.   const sh = ss.getActiveSheet();
  6.   const blobushka = getBlob(ss, sh);  
  7.    
  8.   const file = DriveApp
  9.     .createFile(blobushka)
  10.     .setName(`файл от ${Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'YY-MM-dd')}`);
  11.  
  12.   FOLDER_URL ? file.moveTo(DriveApp.getFolderById(getIdFromUrl(FOLDER_URL))) : ''
  13.  
  14.   Browser.msgBox(`А вот и ваш файл: ${file.getUrl()}`);
  15. }
  16.  
  17. function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }
  18.  
  19. function getBlob(ss, sh) {
  20.   var url = "https://docs.google.com/spreadsheets/export?id=" + ss.getId() + "&gid=" + sh.getSheetId() + "&exportFormat=xlsx"
  21.   var token         = ScriptApp.getOAuthToken();
  22.   var response      = UrlFetchApp.fetch(url, {
  23.     headers: {
  24.       'Authorization': 'Bearer ' +  token
  25.     }
  26.   });
  27.   return response.getBlob();
  28. }
  29.  
  30. function onOpen(e){
  31.   SpreadsheetApp.getUi()
  32.   .createMenu("С к р и п т ы")
  33.   .addItem("превратить выбранный лист в XLSX", "driveIt")
  34.   .addToUi();
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement