# gScript for static date

**URL:** https://community.glideapps.com/t/gscript-for-static-date/37144
**Category:** Ask for Help
**Created:** [January 14, 2022, 2:41am UTC](https://community.glideapps.com/t/gscript-for-static-date/37144 "2022-01-14T02:41:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![gp9293](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gp9293/32/16743_2.png) [@gp9293](https://community.glideapps.com/u/gp9293)
#### Post date: [January 14, 2022, 2:41am UTC](https://community.glideapps.com/t/gscript-for-static-date/37144/1 "2022-01-14T02:41:30Z")

</div>

I received some good ideas on this before but closed the topic as solved, when I should not have. The issue is the following: my sheet receives updates from an external stock price database (think Yahoo Finance). Prices are updated every 15 minutes. When the price exceeds a target value (hard-coded in another field), I would like write a datetime stamp to a third field. But the datetime stamp cannot change even if the price moves below and above the target. during the course of the day/week. Once the target is hit, game over, static timestamp. Appreciate any help.

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 14, 2022, 2:45am UTC](https://community.glideapps.com/t/gscript-for-static-date/37144/2 "2022-01-14T02:45:48Z")

</div>

I remember this. Can you describe the solution that you implemented, and in what way it’s falling down?

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 14, 2022, 3:15am UTC](https://community.glideapps.com/t/gscript-for-static-date/37144/3 "2022-01-14T03:15:37Z")

</div>

Here… imagine that your sheet looks like this:

 ![Screen Shot 2022-01-14 at 11.12.08 AM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/2/f27b29a927297cf443568035a79a1722ea1d83cd.png)

Then the following code should do what you want:

```auto
function check_stock_targets() {
  // Replace each of the below with actual column/sheet names
  const SHEET_NAME = 'Stocks'; 
  const SYMBOL = 'Symbol';
  const CURRENT = 'Current Price';
  const LAST_UPDATE = 'Last Update';
  const TARGET = 'Target';
  const TIME_LOCK = 'Target Hit At';

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(SHEET_NAME);
  var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues().shift();
  var symbol_index = headers.indexOf(SYMBOL);
  var current_index = headers.indexOf(CURRENT);
  var last_update_index = headers.indexOf(LAST_UPDATE);
  var target_index = headers.indexOf(TARGET);
  var time_lock_index = headers.indexOf(TIME_LOCK);

  var data = sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
  var current_row = 2;

  data.forEach(function (row) {
    var time_lock = row[time_lock_index];
    if (time_lock === '') {
      var current = row[current_index];
      var target = row[target_index];
      if (current >= target) {
        var last_update = row[last_update_index];
        var symbol = row[symbol_index];
        console.log("Target for %s hit at %s, locking", symbol, last_update);
        sheet.getRange(current_row,time_lock_index+1).setValue(last_update).setNumberFormat("yyyy-mm-dd hh:mm");
      }
    }
    current_row++;
  });
}

```

I would run that as a timed trigger, perhaps once every 5 minutes.

 ![Screen Shot 2022-01-14 at 11.14.59 AM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/6/8/68d3db9b5c91da48a9b40ef86964f495f903e3b8.png)

---

<div class="post-metadata">

### Author: ![gp9293](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gp9293/32/16743_2.png) [@gp9293](https://community.glideapps.com/u/gp9293)
#### Post date: [January 14, 2022, 3:42am UTC](https://community.glideapps.com/t/gscript-for-static-date/37144/4 "2022-01-14T03:42:45Z")

</div>

Looks good. I will try

---

<div class="post-metadata">

### Author: ![gp9293](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gp9293/32/16743_2.png) [@gp9293](https://community.glideapps.com/u/gp9293)
#### Post date: [January 14, 2022, 6:03pm UTC](https://community.glideapps.com/t/gscript-for-static-date/37144/5 "2022-01-14T18:03:39Z")

</div>

Well, I am not surprised Darren. It works perfectly. Thank you.

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)
#### Post date: [January 15, 2022, 6:03pm UTC](https://community.glideapps.com/t/gscript-for-static-date/37144/6 "2022-01-15T18:03:54Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
