55916

“Cannot call SpreadsheetApp.getUi() from this context” error while not using getUi() on time-based t

<h3>Question</h3>

I am trying to run a function every night that checks a list of dates and does work if it finds that a date has passed and all the checkboxes on that row are checked. Every day, though, I get an email saying

<blockquote>

"Cannot call SpreadsheetApp.getUi() from this context. (line 172, file "Code")".

</blockquote>

The weird thing is that I don't use getUi() anywhere in my CheckHireDates function and the line that it specifies is not even in the function that is supposed to run. Line 172 is in my onEdit function which also doesn't use getUi(). Can anybody help me understand why I'm getting this error?

I did use getUi in my onOpen function, so I commented it out when I started having this problem but it still didn't fix anything.

function CheckHireDates() { var spreadsheet = SpreadsheetApp.getActive(); var PaycomSheet = spreadsheet.getSheetByName('Paycom'); var TechSheet = spreadsheet.getSheetByName("Tech Key"); var SoftwareTracker = spreadsheet.getSheetByName('Software Tracking'); var range = "R2:R"; var Hvals = PaycomSheet.getRange("H2:H").getValues(); var Hlast = Hvals.filter(String).length; var data = PaycomSheet.getRange(range).getValues(); var today = new Date().toLocaleDateString(); for(var i = 0; i < Hlast;i++) { Hvals[i].toLocaleString(); if(Hvals[i] <= today && (PaycomSheet.getRange('R' + (i+2)).getValue() == true)) { var fullName = PaycomSheet.getRange('D' + (i+2)).getValue(); var techRow = findMatchingRow(fullName, "Tech Key"); var softwareRow = findMatchingRow(fullName, "Software Tracking"); var position = PaycomSheet.getRange('G' + (i+2)).getValue(); TechSheet.getRange('E' + techRow).setValue(1); SoftwareTracker.getRange('G' + softwareRow).setValue(1); if (position == "Pre-Employment, Initial") { position = "Initial"; } else if (position == "Pre-Employment, Ace") { position = "Route"; } else if (position == "Pre-Employment, Expert") { position = "Route"; } else { Logger.log("Position not known"); } TechSheet.getRange('G' + techRow).setValue(position); SoftwareTracker.getRange('D' + softwareRow).setValue(position); SoftwareTracker.getRange('H' + softwareRow + ':M' + softwareRow).setValue(2); if (position !== "Initial") { SoftwareTracker.getRange('N' + softwareRow).setValue(2); } } }

}


<h3>Answer1:</h3>

I had that problem and found it involved another google script in the project set up by a different user. The error message had a mixture of details from my script and the other script:

<blockquote>

<em>myFunctionName</em> Cannot call SpreadsheetApp.getUi() from this context. (line 2, file "<em>OtherScript</em>")

</blockquote>

Line 2 of the <em>other</em> script did use getUi()

var app = SpreadsheetApp.getUi();

It seemed that when my script ran (triggered by onChange event) then the other script would also get run (maybe also triggered by a change event). The other script set up some UI elements, some simple menus. Normally that caused no problem. However, SpreadsheetApp.getUi() only works in the context of there being a current instance of an open editor (see https://developers.google.com/apps-script/reference/base/ui). So if a change event happened without an open editor then it fails, causing the error.

I resolved the problem by slightly modifying the other script to catch the problem:

try { app = SpreadsheetApp.getUi(); } catch (error) {} if (app != null) { //Use app... }

A different approach that might also have worked is to speak with the person and how their script was triggered, and see if they'd change it to trigger by onOpen event that just occurs when someone opens a spreadsheet and hence there is a Ui context.

So, I think your problem would be coming from SpreadsheetApp.getUi() in a different script in the project. See if the error message mentions a different file, like mine did. When your script runs in the night, there would be no context, which explains why the error occurs at that time.

来源:https://stackoverflow.com/questions/56008432/cannot-call-spreadsheetapp-getui-from-this-context-error-while-not-using-get

Recommend

  • “Cannot call SpreadsheetApp.getUi() from this context” error while not using getUi() on time-based t
  • “Cannot call SpreadsheetApp.getUi() from this context” error while not using getUi() on time-based t
  • Catch and Continue? C#
  • Changing grid template columns breaks layout
  • Python tkinter, Making two text widget's scrolling synchronize
  • Get Youtube VideoID details in array
  • Java default version doesnt reflect after PATH change
  • ScalaFx: Event Handler with First Class Function
  • jquery animation delay when scrolled
  • How to fit a 2D ellipse to given points
  • format lookupset expression
  • Inject Javascript code into a web page
  • How do I redirect an user back to the page they were trying to access once they log in? (Django)
  • Given a dom element, how could I flatten it in jquery and/or JavaScript?
  • Website Scraping Using PHP
  • How do I make a Google Sheet script send an email when a specific cell's value changes?
  • How to run python3.7 based flask web api on azure
  • Fixing corrupt encoding (with Python)
  • How to add multiple legend titles (columns) in ggplot
  • Hibernate Idempotent Update
  • Google Apps Script fails to generate image from EmbeddedChartBuilder
  • using JQuery autocomplete from database with PHP (CodeIgniter)
  • Simultaneous animation when entering editing mode of UITableViewCell
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • How to convert days into months using datetime in Python3?
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • Delphi Prism getting Unknown Identifier “DllImport” error
  • matrix multiplication apache pig
  • multiple button click in asp.net MVC 3
  • Firebase: How to read from external DB?
  • Running R's aov() mixed effects model from Python using rpy2