ABC
ABC

Reputation: 3

Why is google.script.run is slow?

So I have a sheet where I added a Custom Menu (that I have searched and found in google).

The Custom Menu shows a dialog (see image below) It returns all the data validation I have in a specific cell, each of which has a checkbox so I can select multiple options and return it in a single cell.

see image here

But the FILL CURRENT CELL button runs slow!

I have here these set of codes:

code.gs

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu('Custom Menu')
  .addItem('Show Dialog', 'showDialog')
  .addToUi();
}
function showDialog() {
  var html = HtmlService.createTemplateFromFile('page').evaluate();
  SpreadsheetApp.getUi()
  .showSidebar(html);
}
var valid = function(){
  try{
    return SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1Bw-CW5JZX-MEjPKhrWSNA2zWkqJTR6W3yM2yDrqjE_0/edit#gid=0').getRange('B1').getDataValidation().getCriteriaValues()[0].getValues();
  }catch(e){
    return null
  }
}
function fillCell(e){
  var s = [];
  for(var i in e){
    if(i.substr(0, 2) == 'ch') s.push(e[i]);
  }
  if(s.length) SpreadsheetApp.getActiveRange().setValue(s.join(', '));
}

page.html

<div style="font-family: arial;">
<? var data = valid(); ?>
<form id="form" name="form">
<? if(Object.prototype.toString.call(data) === '[object Array]') { ?>
<? for (var i = 0; i < data.length; i++) { ?>
<? for (var j = 0; j < data[i].length; j++) { ?>
<input type="checkbox" id="ch<?= '' + i + j ?>" name="ch<?= '' + i + j ?>" value="<?= data[i][j] ?>"><?= data[i][j] ?><br>
<? } ?>
<? } ?>
<? } else { ?>
<p>Maybe current cell doesn't have <a href="https://support.google.com/drive/answer/139705?hl=en">Data validation...</a></p>
<? } ?>
<br>
<br>


<input type="button" value="Fill Current Cell" onclick="google.script.run.fillCell(this.parentNode)" />
<input type="button" value="Clear Selections" onclick="reset()" />
</form>
</div>

Can someone please help me so the FILL CURRENT CELL button runs faster?

Upvotes: 0

Views: 1119

Answers (1)

Raserhin
Raserhin

Reputation: 2676

Just to leave an answer:

Take into account that google.script.run need to interact with server every time is called, therefore you will have some performance issues compared with running the same function directly from the editor.

So if you are experiencing little time difference like you have stated in the comments don't worry as it just normal behavior.

Upvotes: 1

Related Questions