Home > Blockchain >  How do I make this function in Google sheet run "every minute"?
How do I make this function in Google sheet run "every minute"?

Time:02-28

What I have:
1_I have timezone in IANA format like Asia/Kolkata, Asia/Amman and so on.

2_I also have the below code pasted in script editor to solve my first problem. It is prepared by user ASyntuBU. I am really thankful to this user. Now, I have a new requirement.

function getTime(timeZone) {
  return Utilities.formatDate(new Date(), timeZone, 'MM/dd/YYYY hh:mm a');
}

What I want:
1_I wish to get the local time of each timezone refreshed every minute just as now() does, that is no reload of the web-page.

2_Also, I want the output in the cell in Date and Time format so that I can subtract two "time" output.
PS I am not from coding background but I will do my best to do as instructed. Please help

Edit 2:
Point 2 solution - This has helped me to get output in date format. If this is correct, how do I get it solution for point 1

function getTime(timeZone) {
  var a = Utilities.formatDate(new Date(), timeZone, 'MM/dd/YYYY hh:mm a');
var b  = new Date(a)
return b
}

Edit 3:
It does not refresh every minute. What is the syntax error? Please help

function getTime(timeZone) {
  var a = Utilities.formatDate(new Date(), timeZone, 'MM/dd/YYYY hh:mm a');
var b  = new Date(a)
 return b
ScriptApp.newTrigger("getTime")
  .timeBased()
  .everyMinutes(1)
  .create();
}

CodePudding user response:

Try for instance

function clock(){
  var chk = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange('A1')
  chk.setValue(!chk.getValue())
}
function getTime(timeZone) {
  var a = Utilities.formatDate(new Date(), timeZone, 'MM/dd/YYYY hh:mm a');
  return a
}
function setTrigger() {
  ScriptApp.newTrigger("clock")
  .timeBased()
  .everyMinutes(1)
  .create();
}

and put a dummy parameter in your custom function as a checkbox that will change value every minutes

=getTime(A2,$A$1)

enter image description here

  • Related