0

I have a single IMPORTDATA formula in Cell A1 of a Google Sheet named "test-r" that I would like to be able to update the cell on a specific interval -- anywhere from seconds to hours.

After a good amount of research, I landed upon this suggestion in a previous post, but I am not having much success with it.

Here is how I modified the script in that post for my IMPORTDATA formula in Cell A1 of my sheet/tab name of "test-r"

function forceEval(sheetName, Row, Col){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("test-r");
  var orig = sheet.getRange(1,1).getFormula(); 
  var temp = orig.replace("=", "?");
  sheet.getRange(row,col).setFormula(temp); 
  SpreadsheetApp.flush();
  sheet.getRange(row,col).setFormula(orig); 
}

function onEdit(e){
    forceEval("test-r", 1, 1)
}

This has got to be 'operator error' on my part and I am new to this.

At the same time, I don't know if there is a more simplistic script for accomplishing my goal.

Any assistance would be appreciated.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
blafarm
  • 69
  • 1
  • 10

2 Answers2

0
  • You want to update the function of cell "A1" every specific interval time.

If my understanding is correct, how about this modification?

Modification points:

  • In your script, OnEdit event trigger is used. In this case, when the cell is edited, the trigger is fired.
    • I think that this might not be suitable for your situation.
    • How about using the time-driven trigger?
  • There are some misspellings in your script. By this, variables are not used, and several error occur.

When above points are reflected to your script, it becomes as follows.

Modified script 1:

function forceEval(){ // Modified
  var sheetName = "test-r";
  var cell = "A1";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName); // Modified
  var orig = sheet.getRange(cell).getFormula(); // Modified
  var temp = orig.replace("=", "?");
  sheet.getRange(cell).setFormula(temp); // Modified
  SpreadsheetApp.flush();
  sheet.getRange(cell).setFormula(orig); // Modified
}
  • After you modified the script, in order to execute the function of forceEval() every specific interval time, please install the time-driven trigger.
  • In this script, #NAME? is displayed for an instant. If you don't want to do this, how about the following sample script?

Modified script 2:

function forceEval(){
  var sheetName = "test-r";
  var cell = "A1";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getSheetByName(sheetName).getRange(cell);
  var formula = range.getFormula();
  range.clearContent();
  SpreadsheetApp.flush();
  range.setFormula(formula);
}

Install time-driven trigger:

In order to install the time-driven trigger, you can manually install it.

Also you can install it using the script. The following script installs the time-driven trigger for the function of forceEval. In this sample script, when setTrigger() is run, the function of forceEval() is run every 10 minutes. By this, the formula of the cell "A1" is updated every 10 minutes.

function setTrigger() {
  var minutes = 10; // In this case, as a sample value, it's 10 minutes.
  var functionName = "forceEval";

  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < triggers.length; i++) {
    if (triggers[i].getHandlerFunction() == functionName && triggers[i].getTriggerSource() == ScriptApp.TriggerSource.CLOCK) {
      ScriptApp.deleteTrigger(triggers[i]);
    }
  }
  ScriptApp.newTrigger(functionName).timeBased().everyMinutes(minutes).create();
}

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Tanaike, thanks very much for that excellent detailed response. I have copied your "Modified script 2" and the script for installing the time-driven trigger exactly as you posted them -- with one modification of changing the update to every minute. I saved it, ran it, and authorized it -- but it is not updating the formula in the cell automatically. I did not insert the script within: "function myFunction() {}" However, every time I run the script manually (using the script Run button), it does update. Any idea why this might not be working? Thanks – blafarm Jun 27 '19 at 17:00
  • @blafarm Thank you for replying. I apologize for the inconvenience. From your replying, I cannot understand your current situation. This is due to my poor skill. I deeply apologize for this. So in order to confirm your situation, can you share your Spreadsheet with your current script for replicating your issue? Of course, please remove your personal information. By this, I would like to confirm your situation. If you can cooperate to resolve your issue, I'm glad. – Tanaike Jun 27 '19 at 23:37
  • @blafarm Unfortunately, in the current stage, more information from you it is required to think of your current issue. Because in my environment, I could confirm that the sample script worked. In order to think of your current issue, can you cooperate? If you can do it, please share your Spreadsheet with your current script for replicating your issue. I would like to confirm your issue. – Tanaike Jul 04 '19 at 05:53
0

function Worksheet() { var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("WORKING SHEET"); var queryString = Math.random(); var cellFunction '={IMPORTRANGE("15MDYUIVjVcEs_7XI0Cbfo0FleO_Lvmbav_oybFgPUI8","WORKING SHEET!A2:Y10000");IMPORTRANGE("15MDYUIVjVcEs_7XI0Cbfo0FleO_Lvmbav_oybFgPUI8","WORKING SHEET!A10001:Y12000")}';//IMPORTRANGE FORMULA// sheetName.getRange('A2').setValue(cellFunction); };

  • Use the trigger Next to run button ==> Choose the function to run thatis(Worksheet)==>Choose which deployment should run(leave it as Head)==>Select event source(Time-Driven)==>Select type of time based trigger(Minutes Timer)==>Select minute interval(Everyminute) Than save. After that go to run select the function(Worksheet) and run for onetime it will update this importrange formula in specific cell and thereafter trigger will keep on updating this cell for every minute. – Sumit Kumar Mukherjee Mar 21 '22 at 19:39
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 22 '22 at 09:00