Force recalculation of importrange by reinstating the cell formula
Required:
Imported Data - Your target where everything will be copied to.
Imported Data Config - Has the following fields:
+---------------------------------------------+--------------------------------+
| A | B |
+---------------------------------------------+--------------------------------+
| Import Data from Spreadsheet with the key: | key |
| Import Data from Spreadsheet between range: | A:AA |
| Imported Data select columns: | SELECT * |
| Imported Data criteria: | WHERE Col15 contains 'Offered' |
| Imported Data should by ordered by column: | ORDER BY Col1 |
+---------------------------------------------+--------------------------------+
Script:
function onOpen() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
}
catch(err)
{
Browser.msgBox(err);
}
var configsheet = ss.getSheetByName("Imported Data Config");
var configkey = configsheet.getRange("B1").getValue();
var configrange = configsheet.getRange("B2").getValue();
var configselect = configsheet.getRange("B3").getValue();
var configwhere = configsheet.getRange("B4").getValue();
var configorderby = configsheet.getRange("B5").getValue();
var importedsheet = ss.getSheetByName("Imported Data");
importedsheet.getRange("A1").setValue('=QUERY(IMPORTRANGE("' + configkey + '","' + configrange + '"),"' + configselect + ' ' + configwhere + ' ' + configorderby + '")');
SpreadsheetApp.flush();
// Solution of sourcecode is: http://stackoverflow.com/questions/13631584/how-can-i-force-a-recalculation-of-cell-using-importrange-function-in-a-google-s
// OnOpen Trigger: https://developers.google.com/apps-script/understanding_triggers
// Active Spreadsheet solution: https://productforums.google.com/forum/#!topic/docs/XIY0WNX0uL8
Browser.msgBox("Sync Complete!");
}
This allows you to change your formula without editing the script, and make it easier to transfer the script across various sheets.