Max number of script in a spreadsheet

77 Views Asked by At

In my spreadsheet, I have inserted 35 project in GAS to generate google doc from the same sheet with the placeholder present in the docs.
The script takes the fields from each row in the sheet the script is contained in and using a Google Doc template (identified by TEMPLATE_ID) creates a PDF doc. The fields replace the place-holders in the template. The place-holders are identified by having a % either side, e.g. %Name%. It is invoked by the “Create PDFs” menu.
Naturally I have created a project for each Google Doc template (because I need 35 different doc from 35 different templates) and so in my sheet i have about 35 custom voice in menu that activate each script. Sometimes some scripts not appears all together and so I must to reload the sheet to find the script I need.
Why? There is a way to avoid this?
The script is this:

var TEMPLATE_ID = 'xxxxxxxx';

var PDF_FILE_NAME = '';

var RESULTS_FOLDER_ID = 'xxxxxxx';

var SENT_COLUMN_NAME = 'Date Sent';

var FILE_NAME_COLUMN_NAME = 'File Name';

var EMAIL_COLUMN_NAME = 'Email';

var DATE_FORMAT = 'yyyy/MM/dd';

var DATE_TIME_FORMAT = 'yyyy/MM/dd HH:mm:ss';

function sendMultiplePdfs() {

  var ui = SpreadsheetApp.getUi();

  if (TEMPLATE_ID === '') {    
    throw new Error('TEMPLATE_ID needs to be defined in Code.gs');
    return;
  }

  var templateFile = DriveApp.getFileById(TEMPLATE_ID); 
  var activeSheet = SpreadsheetApp.getActiveSheet();
  var ranges = activeSheet.getActiveRangeList().getRanges();
  var activeRows = [];

  ranges.forEach(function(range) {
    activeRows.push(range.getValues());
  })

Update:
Solved by replacing the Template ID taking the value dynamically from a cell:

var ss = SpreadsheetApp.openById("ID_OF_THE_SS").getSheetByName("SHEET_NAME");
var TEMPLATE_ID = ss.getRange("RANGE_OF_THE_ID").getValue();
1

There are 1 best solutions below

7
Wicket On BEST ANSWER

AFAIK there isn't a max number of projects that can be bounded to a G Suite editor document but having simple triggers on them could cause problems due to a "race - condition"

Rhetoric question

If there are multiple onOpen each of them creating a custom menu, will all of them be displayed correctly always on the same order?

One approach is to "parameterize" your project, i.e. create 35 functions to call a parameterized function receiving the template id and other parameters

Those 35 functions could be called from a menu, from images, among other ways to call them.


In Google Apps Script / JavaScript a function looks like this:

function doSomething(){
  console.info('Hello world!');
}

the parameterized version could be

function saySomething(message){
   console.info(message);
}

Then you call saySomething by passing a value for the parameter

function greetTheWorld(){
  var message = 'Hello world!';
  saySomethin(message);
}

In the case of this project you might put all the global variables in an array of object, having one object for each template.

var settings = [

  { /* First template */

    TEMPLATE_ID:'xxxxxxxx',

    PDF_FILE_NAME:'',

    RESULTS_FOLDER_ID:'xxxxxxx',

    SENT_COLUMN_NAME:'Date Sent',

    FILE_NAME_COLUMN_NAME:'File Name',

    EMAIL_COLUMN_NAME:'Email',

    DATE_FORMAT:'yyyy/MM/dd',

    DATE_TIME_FORMAT:'yyyy/MM/dd HH:mm:ss'

  },
  { /* Second template */

    TEMPLATE_ID:'xxxxxxxx',

    PDF_FILE_NAME:'',

    RESULTS_FOLDER_ID:'xxxxxxx',

    SENT_COLUMN_NAME:'Date Sent',

    FILE_NAME_COLUMN_NAME:'File Name',

    EMAIL_COLUMN_NAME:'Email',

    DATE_FORMAT:'yyyy/MM/dd',

    DATE_TIME_FORMAT:'yyyy/MM/dd HH:mm:ss'

  }
]

The pass the corresponding object of the parameterized function which previously was prepared to receive the corresponding values as an object.

Reference