Distributing spreadsheets and Collecting data

Enabling each Google Apps Script

Go to https://script.google.com/

There is a bit of dance to get your script to run. To “run” it the first time, you'll have a bit of a dance.

  • “Authorization required”–> “Review permissions”–>choose your own account
  • “Google hasn’t verified this app”–> find and click on “Advanced”
  • “Go to project (unsafe)”
  • “Allow”

collecting data

This script creates a number of duplicate google sheet files and saves a way to import all the data from sheets. In this case, “data1” and “data2” are duplicates of “template”. The data from each of the duplicates can then be accessed in the google sheet called “summaryData”.

The benefit of this strategy is that if a data# file is accidently corrupted or ineptly entered, it will not effect the other files. Also access to one file, data2 say, require access to other data files. Another point: probably a good idea to have the “template” file in a special folder since all the duplicates will be created in the same folder–will make it easier for deleting them after you are done.

Here's the quick and dirty…or perhaps I should say lightweight?

function centralSheet() {   
  // a bunch of file names can be in a spreadsheet can easily be added here by using =textjoin(",",true,a2:a#) 
  var fileName =["data1", "data2"]; 
  var keepId =[];   
  var template = DriveApp.getFilesByName("template").next(); //assumes "template" file with "data" sheet
  for (let i=0;i<fileName.length;i++){     
    var dup = template.makeCopy(fileName[i]);     
    keepId.push(dup.getId());     
    console.log("created",fileName[i],keepId[i]);   
  }

  var summarySheet = SpreadsheetApp.create("summaryData");
  // add an importrange for each new file
  for (let i=0;i<fileName.length;i++){     
     let spot = 2+i*30; //each sheet will have 30 rows of data with room for the title row
     var r = "a"+spot+":c"+spot;
     var val = [[fileName[i], 
                keepId[i],
                '=importrange("https://docs.google.com/spreadsheets/d/'+keepId[i]+'","data!a2:g30")']]; 
                // in this case, data!a2:g30 is the interesting part of each sheet.. 
     console.log("createCells",r,val);
     summarySheet.getRange(r).setValues(val);
  }
}

Since google's file Id is in the second column, any data can be accessed somewhere else using a different “importrange”. One further step, every third column cell in “summaryData” will have to be clicked “Allow” before the data in imported…

distributing files

A critical benefit of creating multiple files is to share and distribute the data files so that more people can work on the data entry. A list of files and gmails can be used to add editors to the data files.

This script is bare-bones way of sharing the files.

function shareSheets() {
    // using textjoin in a spreadsheet can make assigning data files simpler and less error prone
    var share = [ ["data1","jim.morey@gmail.com"], ["data2","jack.morey@gmail.com"] ];  // what to share with who
    for(let i=0;i<share.length;i++){
      var afile = DriveApp.searchFiles("title='"+share[i][0]+"'"); // find the sheet
      if (afile.hasNext()){
          let found = afile.next();
          found.addEditor(share[i][1]);
          console.log("worked",found.getName(),found.getUrl());
      } else {
        console.log("problem with",share[i][0],share[i][1]);
      }
    }
}
apps_script.txt · Last modified: 2021/10/01 13:18 by morey
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki