Google Docs will be full on June 1st. We write a script to bypass this limitation

Background

Google will change its data retention policy effective June 1, 2021 . In short: documents and photos will now become full-fledged and will be taken into account in the total quota of 15GB. In addition, if your account has been inactive for more than two years, Google may delete your data.





I often work with Google Docs, and with heavy use, the disk quota will run out pretty quickly. But there is good news: documents created before June 2021 1 , the so-and remain weightless , so you do not get a quota violation overnight.





I immediately had the idea to make the documents "in stock". Below I will tell you how this can be done without spending a lot of time and effort.





Writing a script that creates documents

I will write the script using Google Apps Script.





Create a new Google Spreadsheet, go to the script editor ( Tools - Script Editor ).





Create three files





  • main.gs - the main code for creating files





  • menu.gs - code to create a custom menu when opening a table





  • index.html -





menu.gs onOpen(). , . - , .





function onOpen(e) { 												//    
  SpreadsheetApp.getUi() 										//   
      .createMenu('')										//  
      .addItem(' ', 'main') //   
      .addToUi();														//    
}
      
      



main.gs main(), .





function main() { //  -  
  //  HTML   
  let template = HtmlService.createTemplateFromFile(`index`);
  //     HTML 
  SpreadsheetApp.getUi()
    .showModelessDialog(template.evaluate(),` ...`);
}
      
      



index.html. HTML , . , ..





index.html
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <script>
      let timer = function(){ //  
        //   
        let now = (new Date()).getTime();
        
        //     - 1   
        setInterval(function(){
          //   . now   - .
          let time = (new Date()).getTime() - now;
          //  
          let minutes = Math.floor(time/60000);
          //  
          let seconds = Math.floor(time%60000/1000);
          //     lifeTime
          updateData("lifeTime", `${minutes<10?"0"+minutes:minutes}:${seconds<10?"0"+seconds:seconds}`);
        },1000);
      };
      
      updateData = function(id, value){ //       id
        let element = document.getElementById(id);
        if (element) {
          element.innerHTML = value;
        };
      };
      
      refreshData = function(){ //   
        google.script.run.withSuccessHandler(function(data){
        updateData("createTables", data.createTables); 	//  :
        updateData("createDocs", data.createDocs);			//  :
        updateData("createForms", data.createForms);		//  
        updateData("createSlides", data.createSlides);	//  
        updateData("log", data.log);										// 
      }).getData(); //   
    };

      setTimeout(setInterval(refreshData, 2000),1000); 	//     2 
      timer();																					//  
      google.script.run.doMagic();								//     
    </script>
  </head>
  <body>
    <div class=".container bg-dark text-white text-center">
      <div class="row">
        <div class="col">
          
        </div>        
        <div class="col">
          
        </div>        
        <div class="col">
          
        </div>        
        <div class="col">
          
        </div>
      </div>
      <div class="row">
        <div class="col" id="createTables">
          0
        </div>
        <div class="col" id="createDocs">
          0
        </div>        
        <div class="col" id="createForms">
          0
        </div>        
        <div class="col" id="createSlides">
          0
        </div>
      </div>
    </div>    
     <div class=".container bg-dark text-white">
      <div class="row">
        <div class="col text-right" id="label_lifeTime">
           :  
        </div>        
        <div class="col  text-left" id="lifeTime">
          00:00
        </div>        
      </div>
    </div>

    <div bg-dark text-white id="label_log">: </div>
    <ul class="list-group" id="log">
    </ul>
  </body>
</html>

      
      



, , :





  • updateData(id, value) - id





  • refreshData() - ,





  • timer() -





main.gs .





function create()
const FILES_TO_CREATE = 50;

function create(filesToCreate = FILES_TO_CREATE, folderId, prefix="file_", app, key) {
  //   -   .
  let props = PropertiesService.getScriptProperties();
  //     data.   
  let data = JSON.parse(props.getProperty(`data`));

  try{
    //    id
    let folder = DriveApp.getFolderById(folderId);
    for(var i=0; i<filesToCreate; i++){ //  filesToCreate 
      //  ,   id
      let ssId = app.create(`${prefix}${(new Date()).getTime()}`).getId();
      //     id
      let ss = DriveApp.getFileById(ssId);
      //    
      folder.addFile(ss);
      //     
      DriveApp.getRootFolder().removeFile(ss);

      //    
      data[key]=1+data[key];
      //   
      props.setProperty(`data`, JSON.stringify(data));
    };
  }catch(err){
    //    -    
    logToHtml(`Error: ${err}`, LOG_TYPES.danger);
  };
  //      
  return +i;
};
      
      



- , , .





create()
function createSheets(key) {
  //  id   
  let folderId =  PropertiesService.getScriptProperties().getProperty(`sheetsFolder`);
  //    
  let count = create(FILES_TO_CREATE, folderId, `sheet_`, SpreadsheetApp, key);
  //   
  logToHtml(`${count} sheets were created`, LOG_TYPES.success);
}

function createDocs(key) {
  let folderId =  PropertiesService.getScriptProperties().getProperty(`docsFolder`);
  let count = create(FILES_TO_CREATE, folderId, `doc_`, DocumentApp, key);
  logToHtml(`${count} docs were created`, LOG_TYPES.success);
}

function createForms(key) {
  let folderId =  PropertiesService.getScriptProperties().getProperty(`formsFolder`);
  let count = create(FILES_TO_CREATE, folderId, `form_`, FormApp, key);
  logToHtml(`${count} forms were created`, LOG_TYPES.success);
}

function createSlides(key) {
  let folderId =  PropertiesService.getScriptProperties().getProperty(`slidesFolder`);
  let count = create(FILES_TO_CREATE, folderId, `slide_`, SlidesApp, key);
  logToHtml(`${count} slides were created`, LOG_TYPES.success);
}
      
      



.





function createFolders(){
  //   -   
  let props = PropertiesService.getScriptProperties();

  //   
  let folders = [
    {key:`rootFolder`,   name:``                         },
    {key:`sheetsFolder`, name:`Sheets`, parentFolder:`rootFolder`},
    {key:`docsFolder`,   name:`Docs`,   parentFolder:`rootFolder`},
    {key:`formsFolder`,  name:`Forms`,  parentFolder:`rootFolder`},
    {key:`slidesFolder`, name:`Slides`, parentFolder:`rootFolder`},
    ];

  //      
    folders.forEach(folder=>{
      if (!props.getProperty(folder.key)){ //     
        //    rootFolder,   ,    
        let parentFolder = folder.parentFolder?DriveApp.getFolderById(props.getProperty(folder.parentFolder)):DriveApp.getRootFolder();
        //  
        let folderId = parentFolder.createFolder(folder.name).getId();
        //    
        props.setProperty(folder.key, folderId);
      };
    });
}
      
      



:





,
function doMagic(){
  let props = PropertiesService.getScriptProperties();

  //  
  let data = {
    createTables:0, 
    createDocs:0, 
    createForms:0, 
    createSlides:0, 
    startTime:new Date(),
    log:``,
  };

  //     
  props.setProperty(`data`, JSON.stringify(data));
  
  try{
    createFolders(); 							//  
    createSheets(`createTables`);	//  
    createDocs(`createDocs`);			//  
    createForms(`createForms`);		//  
    createSlides(`createSlides`);	//  
    
    // ,   
    SpreadsheetApp.getUi().alert(`!`);
  }catch(err){
    //     
    SpreadsheetApp.getUi().alert(`! ${err}`);
  };
};
      
      



- .





function getData(){ //    
  //   -
  let props = PropertiesService.getScriptProperties();
  //       
  let data = JSON.parse(props.getProperty(`data`));
  return data; // 
};
      
      



:





const LOG_TYPES = { //  .   bootstrap
  primary:   "primary",
  secondary: "secondary",
  success:   "success",
  danger:    "danger",
  warning:   "warning",
  info:      "info",  
};

function logToHtml(log, type = LOG_TYPES.primary){
  //  
  let data = getData();
  //  li  (    )  
  data.log+=`<li class="list-group-item text-${type}">${log}</li>\n`;
  //  
  let props = PropertiesService.getScriptProperties();
  props.setProperty(`data`, JSON.stringify(data));
};
      
      



. Google - . , .





Modal window for visualizing progress

  • 6 . . , HTML API,





  • . Exception: : docs create.






TL;DR

I put all the above into a table that you can copy to yourself ( File - Create a copy ), run ( Menu - Create files ) and get several hundred files to your disk. Repeat the procedure if necessary.





Thanks for attention. I would be glad to receive feedback on the code. Good luck!








All Articles