"Cannot Call SpreadsheetApp.getUi() from context" while trying to run manually

2k Views Asked by At

I am trying to access the UI object in Apps Script the same way I always do, inf fact the code I am using is re-used code that I have already used before and it worked fine, for some reason I am getting an error message when I try to run my script saying that I "Cannot Call the .getUI()" method from this context, but that usually presents when using a trigger, but I am not, I am just running it from the IDE.

Does anyone know if there is an update or something that is messing with this? maybe I'm doing something wrong? If so I have pasted the code below

function addProject() {
  var ui = SpreadsheetApp.getUi();

  //var listDepartment = ss.getRangeByName("ref_departments").getValues().filter(String);
   // var projectSheet = ss.getSheetByName('Project Overview');

  var nameResponse = ui.prompt("What is the name of this project?");
  var projectName = nameResponse.getResponseText(); 

  var roleResponse = ui.prompt("How Many Resource/Roles are required for this project?");
  var roleCount = parseInt(roleResponse.getResponseText());

//   var validateDepartment = SpreadsheetApp.newDataValidation()
//  .requireValueInList(listDepartment)
//  .build();

 // var color = ss.getRangeByName("refColor").getValue();

  var lastRow = projectSheet.getLastRow();
  var lastCol = projectSheet.getMaxColumns();

  projectSheet.getRange(lastRow+1, 1)
  .setValue(projectName)
  .setVerticalAlignment("middle")
  .setHorizontalAlignment("Center")
  .setFontSize(16)
  .setWrap(true)
  .setFontWeight("bold")
  .copyTo(projectSheet.getRange(lastRow+2, 1, roleCount-1, 1));

  projectSheet.getRange(lastRow+1, 2, roleCount, 1).setDataValidation(validateDepartment);
  projectSheet.getRange(lastRow+1, 1, roleCount,lastCol).setBackground(color);

  var rowPos = lastRow + 1;

//  projectSheet.getRange(lastRow+1, 6).setFormula('=if(D'+rowPos+'="","-",if(counta(G'+rowPos+':KV'+rowPos+')<D'+rowPos+',D'+rowPos+'-counta(G'+rowPos+':KV'+rowPos+'),"Full Allocation"))')
//  .copyTo(projectSheet.getRange(lastRow+2,6,roleCount-1,1));
//  
  projectSheet.getRange(lastRow+roleCount+1, 1).setValue("-");

//  if( color == "#efefef")
//  {
//   ss.getRangeByName("refColor").setValue("#ffffff") 
//  }
//  else ss.getRangeByName("refColor").setValue("#efefef");
//  

  /* Project Name */   variablesSheet.getRange(variablesSheetLastRow+1,1).setValue(projectName);
  /* Start Cell*/     variablesSheet.getRange(variablesSheetLastRow+1,2).setValue(projectSheet.getRange(lastRow+1, 1).getA1Notation());
  /*End Cell */        variablesSheet.getRange(variablesSheetLastRow+1,3).setValue( projectSheet.getRange(lastRow+1+roleCount, 1).getA1Notation());
  /* Number of Jobs */ variablesSheet.getRange(variablesSheetLastRow+1,4).setValue(roleCount);
  /* Date Cell */      variablesSheet.getRange(variablesSheetLastRow+1,5).setValue(projectSheet.getRange(lastRow, 7).getA1Notation());



}

function staffHeaders(){

    var ss = SpreadsheetApp.getActive();
  var utilSheet = ss.getSheetByName("Staff Utilization");

  var staffNames = ss.getRangeByName("ref_staff").getValues().filter(String);
  utilSheet.getRange(1,1,1,utilSheet.getMaxRows()).clearContent();

  for (i = 1; i< staffNames.length ; i++)
  {

    utilSheet.getRange(1, i+1).setValue(staffNames[i]);

  }



}
1

There are 1 best solutions below

0
On BEST ANSWER

The Class UI can't be called from the Apps Script IDE but it could be called from a script run from a custom menu, dialog or clicking an image with a script attached to it.