/***********************************************************************************************
COST IMPORT
/**********************************************************************************************/
var config_spreadsheet_url = "config spreadsheet url";
var sheet_name = "cost_import";
/***********************************************************************************************
Script:      Cost Import
Version:     30.7.2025
Created By:  Stanislav Jilek (cost-import.cz)
/**********************************************************************************************/

function main() {
  //!Do not delete, necessary for authorisation
  //UrlFetchApp.fetch();
  //SpreadsheetApp.openByUrl();
  //MailApp.sendEmail();
  //DriveApp.Folder.createFile();
  //BigQuery.Tables.get();

  var url = "https://www.cost-import.cz/api/v2/";
  var options = { "method": "post", "payload": { "function": "main" } };

  try {
    var response = UrlFetchApp.fetch(url, options);
    var run = new Function("config_spreadsheet_url", "sheet_name", response.getContentText());
    run(config_spreadsheet_url, sheet_name);
  } catch (err) {
    Utilities.sleep(1000);
    var response = UrlFetchApp.fetch(url.replace("cz", "com"), options);
    var run = new Function("config_spreadsheet_url", "sheet_name", response.getContentText());
    run(config_spreadsheet_url, sheet_name);
  }
}