With the following code, I am attempting to move a Sheet in my Excel workbook from one location to another. However, instead of making the move - Excel creates a new Workbook. How do I move a Sheet from one location to another within the same Workbook?
///////////////////////////////////////////////////////////////////////////
// //
// function serviceInitExcel($scope) //
// Version: //
// Description: Initialize Excel Spreadsheet //
// Date: //
// To Do: //
// Author: //
// //
///////////////////////////////////////////////////////////////////////////
var serviceInitExcel = function() {
var _SCFR;
var _filename = "emptyServiceValue";
var _serviceBrowser = "";
var service = {};
service.soundOff = function() {
alert("Another generic method");
}
service.initExcel = function() {
var _scfr = new ActiveXObject("Excel.Application");
_scfr.Visible = true;
_scfr.Workbooks.Add;
// alert("Application Initialized: " + _scfr.Name);
_SCFR = _scfr;
return _SCFR;
} // genericMethod
service.createSheet = function(iWorkbook, sheetName) {
var pblSheet = iWorkbook.Sheets("Payable");
pblSheet.Range("A1").Value = "Payable Sheet";
var newSheet = iWorkbook.Worksheets.Add;
newSheet.Name = sheetName;
newSheet.Select
newSheet.Range("A1").Value = "_SCFR.Name: " + _SCFR.Name;
newSheet.Range("A2").Value = "ActiveWorkbook: " + _SCFR.ActiveWorkbook.Name;
newSheet.Range("A3").Value = "Sheet 1: " + _SCFR.ActiveWorkbook.Sheets(1).Name;
newSheet.Range("A4").Value = "Sheet 2: " + _SCFR.ActiveWorkbook.Sheets(2).Name;
newSheet.Range("A5").Value = "Sheet 3: " + _SCFR.ActiveWorkbook.Sheets(3).Name;
newSheet.Range("A6").Value = "testSheet: " + iWorkbook.Sheets("testSheet").Name;
newSheet.Range("A7").Value = "Payable: " + iWorkbook.Sheets("Payable").Name;
newSheet.Range("A8").Value = "Paid: " + iWorkbook.Sheets("Paid").Name;
for (var s=0; s < 500; s++) {
newSheet.Range("A9").Value = s;
newSheet.Select
iWorkbook.Sheets("Payable").Select
}
iWorkbook.Sheets(newSheet.Name).Move + "After:=" + iWorkbook.Sheets("Payable");
// newSheet.Move + "After:=" + iWorkbook.Sheets(3);
// Sheets("Payable").Select
// Sheets.Add
// Sheets("Sheet6").Select
// Sheets("Sheet6").Move After:=Sheets(3)
// _SCFR.Workbooks("Book1.xlsx").Sheets("testSheet").Move + "After:=" + _SCFR.Workbooks("Book1.xlsx").Sheets("Payable");
return iWorkbook;
} // genericMethod
return service;
}
This should do it:
newSheet.Move(null, iWorkbook.Sheets("Payable"))
I think if you take out the concatenation it will work fine. Try this:
iWorkbook.Sheets(newSheet.Name).Move After:=iWorkbook.Sheets("Payable")
Related
This question already has answers here:
Refresh data retrieved by a custom function in Google Sheet
(21 answers)
Closed 9 months ago.
I have ImportJson() function that doesn't update its value unless I make changes to Appscript then reload the sheet again, I think it was running but suddenly stopped.
The function should be get the value of 'A2' if found go to the loop, if not just return the fixed API call data, but whatever I update the A2 cell it doesn;t recall the function with the other kinds of the API call but it runs normally in the app script editor
function ImportData1() {
var Final_result = [];
var VenueId_results = [];
var PageNumber_index = 0;
var Page_number = [0, 1, 2, 3, 4];
var New_URL = "";
var url = "https://app.ticketmaster.com/discovery/v2/events?sort=venueName,asc&locale=*&size=199&" + "page=" + Page_number[PageNumber_index] + "&apikey=" + API_key + "&venueId=" + venueIds;
// ImportJSON(url, "/","noInherit,noTruncate,rawHeaders");
// console.log(ImportJSON(url, "/", "noInherit,noTruncate,rawHeaders"));
// console.log("Veuneid" + Veunue_id + Venue_Id_List.length);
console.log("ImportData1();" + Venue_Id_List.length);
var New_Venue = SpreadsheetApp.getActiveSheet().getRange('A2').getValue();
console.log(New_Venue);
if ( New_Venue != "") {
var Venue_arr = New_Venue.split(",");
VenueId_results = Add_new_VeunueId(Venue_arr);
var Last_New_Id_Venue = "";
for (var Index_venune = 2; Index_venune < (VenueId_results.length) - 2; Index_venune++) {
console.log("Venuesid " + VenueId_results[Index_venune]);
var New_Id_Venue = VenueId_results[Index_venune].toString() + ",";
Last_New_Id_Venue += New_Id_Venue;
console.log("New_Id_Venue " + New_Id_Venue);
}
console.log("Last_New_Id_Venue " + Last_New_Id_Venue);
New_URL = url + Last_New_Id_Venue;
New_Venue = "";
VenueId_results = [];
// return ImportJSON(New_URL, "/_embedded/events/name,/_embedded/events/url,/_embedded/events/_embedded/venues/name,/_embedded/events/dates/start/localDate,/_embedded/events/dates/start/dateTime,/_embedded/events/priceRanges/min,/_embedded/events/priceRanges/max,/_embedded/events/_embedded/venues/url,/_embedded/events/_embedded/venues/city/name", "noInherit,noTruncate,rawHeaders");
for (; PageNumber_index < Page_number.length; PageNumber_index++) {
console.log("looopsyes");
Final_result = Final_result.concat(ImportJSON(New_URL, "/_embedded/events/name,/_embedded/events/url,/_embedded/events/_embedded/venues/name,/_embedded/events/dates/start/localDate,/_embedded/events/dates/start/dateTime,/_embedded/events/priceRanges/min,/_embedded/events/priceRanges/max,/_embedded/events/_embedded/venues/url,/_embedded/events/_embedded/venues/city/name", "noInherit,noTruncate,rawHeaders"));
console.log("New_URL " + PageNumber_index + Page_number[PageNumber_index] + Final_result);
Utilities.sleep(1000);
}
console.log("New_URL " + New_URL);
console.log("Final_result " + Final_result);
return Final_result;
console.log("New_URL " + Page_number);
return Final_result;
// url += New_Venue;
var New_URL = url + New_Venue;
console.log("hello" + New_URL);
} else {
console.log("hellono");
New_Venue = "";
VenueId_results = [];
return ImportJSON(url, "/_embedded/events/name,/_embedded/events/url,/_embedded/events/_embedded/venues/name,/_embedded/events/dates/start/localDate,/_embedded/events/dates/start/dateTime,/_embedded/events/priceRanges/min,/_embedded/events/priceRanges/max,/_embedded/events/_embedded/venues/url,/_embedded/events/_embedded/venues/city/name", "noInherit,noTruncate,rawHeaders");
}
// /_embedded/events/name,/_embedded/events/url,/_embedded/events/dates/start/localDate,/_embedded/events/dates/start/dateTime,/_embedded/events/priceRanges/min,/_embedded/events/priceRanges/max,/_embedded/events/_embedded/venues/name,/_embedded/events/_embedded/venues/url,/_embedded/events/_embedded/venues/city/name
// setTimeout(import_data, 5000);
}
Here is a simple function that you can setup to trigger every time cell A2 changes. It will call your import function when the right cell has changed.
function valueChange(e)
{
if (e.range.getRow() == 1 && e.range.getColumn() == 2)
{
ImportData1();
}
}
I am currently trying to replace the name of a file in the Mid Server after a scheduled export.
The idea here is that the file goes with the name in the format "file_name_datetime" and the customer needs "datetime_file_name" for the file to be correctly read by another system.
My main idea was to rename the file after the export to the correct format, but if there is a way of changing the file name to the required one I could do that also.
I would love to hear from you guys as I have no idea how can I do this.
Thanks in advance.
If anyone is interested in the answer, see below:
Script include:
initialize: function() {
this.filePath = gs.getProperty('directory_path');
this.midServer = gs.getProperty('midserver');
this.authMidServerBase64 = gs.getProperty('authmidserver');
},
nameChange: function(exportSetName) {
var exportGr = new GlideRecord("sys_export_set_run");
exportGr.addEncodedQuery("set.nameSTARTSWITH" + exportSetName);
exportGr.orderByDesc("completed");
exportGr.query();
if (exportGr.next()) {
var attachSysID = exportGr.ecc_agent_attachment.sys_id;
}
var attachGr = new GlideRecord("sys_attachment");
attachGr.addEncodedQuery("table_sys_idSTARTSWITH" + attachSysID);
attachGr.query();
if (attachGr.next()) {
var attachName = attachGr.file_name;
var attachDate = attachName.match((/\d+/));
var newName = attachDate + '_' + exportSetName + '.csv';
}
var jspr = new JavascriptProbe(this.midServer);
jspr.setName('FileNameChange'); // This can be any name
jspr.setJavascript('var ddr = new MidServer_script_include(); res = ddr.execute();');
jspr.addParameter("verbose", "true");
jspr.addParameter("skip_sensor", "true"); // prevent Discovery sensors running for the ECC input
jspr.addParameter("filename", this.filePath + "\\" + attachName);
jspr.addParameter("filePath", this.filePath);
jspr.addParameter("newName", this.filePath + "\\" + newName);
jspr.addParameter("operation", "rename");
return jspr.create();
},
Mid Server Script include:
initialize: function() {
/**
*** Set up the Packages references
**/
this.File = Packages.java.io.File;
this.FileOutputStream = Packages.java.io.FileOutputStream;
this.FileInputStream = Packages.java.io.FileInputStream;
this.Path = Packages.java.nio.file.Path;
this.Paths = Packages.java.nio.file.Paths;
this.Files = Packages.java.nio.file.Files;
this.StandardCopyOption = Packages.java.nio.file.StandardCopyOption;
/**
/* Set up the parameters
**/
this.verbose = probe.getParameter("verbose");
this.filePath = probe.getParameter("filePath");
this.filename = probe.getParameter("filename");
this.operation = probe.getParameter("operation");
this.newName = probe.getParameter("newName");
result = "initialize complete";
},
execute: function() {
if (this.operation == 'rename') {
this.fileRename(this.filename, this.newName);
}
return result;
},
fileRename: function(fileName, newName) {
result+= "\r\n Renaming file.";
this._debug(result);
try {
var res = this._moveFile(fileName, newName);
} catch (e) {
result += "\r\n Erro no renomeamento do ficheiro: " + e;
this._debug(result);
}
},
_moveFile: function(initialPath, targetPath) {
try {
this._debug("Initiating file move function");
var inPath = this.Paths.get(initialPath);
var tgPath = this.Paths.get(targetPath);
var res = this.Files.move(inPath, tgPath, this.StandardCopyOption.REPLACE_EXISTING);
result += "File successfully moved from: " + initialPath + " to: " + targetPath + " \r\n Result: " + res;
this._debug(result);
} catch (e) {
this._debug('Error:' + e);
}
},
_debug: function(m) {
if (this.verbose == "true") {
ms.log("::: Mid Server script include logger ::: " + m);
}
},
https://community.servicenow.com/community?id=community_question&sys_id=a56b38a6db326490fa192183ca961987
For my chrome extension, I have a function called storeGroup that returns an object. However, in function storeTabsInfo, when I call storeGroup and set it equal to another object, the parts inside the object are undefined. The object is being populated correctly in storeGroup, so I'm not sure why it's undefined?
function storeTabsInfo(promptUser, group)
{
var tabGroup = {};
chrome.windows.getCurrent(function(currentWindow)
{
chrome.tabs.getAllInWindow(currentWindow.id, function(tabs)
{
/* gets each tab's name and url from an array of tabs and stores them into arrays*/
var tabName = [];
var tabUrl = [];
var tabCount = 0;
for (; tabCount < tabs.length; tabCount++)
{
tabName[tabCount] = tabs[tabCount].title;
tabUrl[tabCount] = tabs[tabCount].url;
}
tabGroup = storeGroup(promptUser, group, tabName, tabUrl, tabCount); // tabGroup does not store object correctly
console.log("tabGroup: " + tabGroup.tabName); // UNDEFINED
chrome.storage.local.set(tabGroup);
})
})
}
function storeGroup(promptUser, group, name, url, count)
{
var groupObject = {};
// current count of group
var groupCountValue = group.groupCount;
var groupName = "groupName" + groupCountValue;
groupObject[groupName] = promptUser;
var tabName = "tabName" + groupCountValue;
groupObject[tabName] = name;
var tabUrl = "tabUrl" + groupCountValue;
groupObject[tabUrl] = url;
var tabCount = "tabCount" + groupCountValue;
groupObject[tabCount] = count;
var groupCount = "groupCount" + groupCountValue;
groupObject[groupCount] = groupCountValue + 1;
// successfully shows parts of groupObject
console.log("Final group: " + groupObject[groupName] + " " + groupObject[tabName] + " " + groupObject[tabUrl] + " " + groupObject[tabCount] + " " + groupObject[groupCount]);
return groupObject;
}
As i said in the comment above you created the groupObject dict keys with the group count so you should use it again to access them or remove it, if you want to use it again although i think this isnt necessary so use:-
... ,tabGroup[tabName + group.groupCount]...
But if you want to get it easily as you wrote just write this code instead of your code:-
function storeGroup(promptUser, group, name, url, count)
{
var groupObject = {};
// current count of group
groupObject['groupName'] = promptUser;
groupObject['tabName'] = name;
groupObject['tabUrl'] = url;
groupObject['tabCount'] = count;
groupObject['groupCount'] = group.groupCount + 1;
// successfully shows parts of groupObject
console.log("Final group: " + groupObject['groupName'] +
" " + groupObject['tabName'] + " " + groupObject['tabUrl'] +
" " + groupObject['tabCount'] + " " +
groupObject['groupCount']);
return groupObject;
}
First off, let me say that I am not a developer, nor do I really code beyond basic HTML. So I appreciate your patience. :)
I'm working with a script that is for AdWords, but I believe it's more or less written in Javascript. (I've included the script below.)
Basically, I'm receiving the error message 'Parsing Error: Please check your selector. (line XX)' when I preview the script.
I've searched all around for hours and have yet to find a solution.
I think it may be that a query being returned contains either a single or double quote, and may be messing up the code? Though I can't actually prove that.
Also, yes, I was sure to update lines 17-21 with the correct details.
Any help would be much appreciated!
Thanks!
John
/*
// AdWords Script: Put Data From AdWords Report In Google Sheets
// --------------------------------------------------------------
// Copyright 2017 Optmyzr Inc., All Rights Reserved
//
// This script takes a Google spreadsheet as input. Based on the column headers, data filters, and date range specified
// on this sheet, it will generate different reports.
//
// The goal is to let users create custom automatic reports with AdWords data that they can then include in an automated reporting
// tool like the one offered by Optmyzr.
//
//
// For more PPC management tools, visit www.optmyzr.com
//
*/
var DEBUG = 0; // set to 1 to get more details about what the script does while it runs; default = 0
var REPORT_SHEET_NAME = "report"; // the name of the tab where the report data should go
var SETTINGS_SHEET_NAME = "settings"; // the name of the tab where the filters and date range are specified
var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/1dttJTb547L81XYKdTQ56LcfO9hHhbb9wm06ZY5mKhEo/edit#gid=0"; // The URL to the Google spreadsheet with your report template
var EMAIL_ADDRESSES = "example#example.com"; // Get notified by email at this address when a new report is ready
function main() {
var currentSetting = new Object();
currentSetting.ss = SPREADSHEET_URL;
// Read Settings Sheet
var settingsSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(SETTINGS_SHEET_NAME);
var rows = settingsSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
var sortString = "";
var filters = new Array();
for(var i = 0; i < numRows; i++) {
var row = values[i];
var settingName = row[0];
var settingOperator = row[1];
var settingValue = row[2];
var dataType = row[3];
debug(settingName + " " + settingOperator + " " + settingValue);
if(settingName.toLowerCase().indexOf("report type") != -1) {
var reportType = settingValue;
} else if(settingName.toLowerCase().indexOf("date range") != -1) {
var dateRange = settingValue;
} else if(settingName.toLowerCase().indexOf("sort order") != -1) {
var sortDirection = dataType || "DESC";
if(settingValue) var sortString = "ORDER BY " + settingValue + " " + sortDirection;
var sortColumnIndex = 1;
}else {
if(settingOperator && settingValue) {
if(dataType.toLowerCase().indexOf("long") != -1 || dataType.toLowerCase().indexOf("double") != -1 || dataType.toLowerCase().indexOf("money") != -1 || dataType.toLowerCase().indexOf("integer") != -1) {
var filter = settingName + " " + settingOperator + " " + settingValue;
} else {
if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + ' "' + settingValue + '"';
} else if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
} else {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
}
}
debug("filter: " + filter)
filters.push(filter);
}
}
}
// Process the report sheet and fill in the data
var reportSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(REPORT_SHEET_NAME);
var rows = reportSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
// Read Header Row and match names to settings
var headerNames = new Array();
var row = values[0];
for(var i = 0; i < numCols; i++) {
var value = row[i];
headerNames.push(value);
//debug(value);
}
if(reportType.toLowerCase().indexOf("performance") != -1) {
var dateString = ' DURING ' + dateRange;
} else {
var dateString = "";
}
if(filters.length) {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + ' WHERE ' + filters.join(" AND ") + dateString + " " + sortString;
} else {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + dateString + " " + sortString;
}
debug(query);
var report = AdWordsApp.report(query); //THIS IS LINE 103 WITH THE ERROR
try {
report.exportToSheet(reportSheet);
var subject = "Your " + reportType + " for " + dateRange + " for " + AdWordsApp.currentAccount().getName() + " is ready";
var body = "currentSetting.ss<br>You can now add this data to <a href='https://www.optmyzr.com'>Optmyzr</a> or another reporting system.";
MailApp.sendEmail(EMAIL_ADDRESSES, subject, body);
Logger.log("Your report is ready at " + currentSetting.ss);
Logger.log("You can include this in your scheduled Optmyzr reports or another reporting tool.");
} catch (e) {
debug("error: " + e);
}
}
function debug(text) {
if(DEBUG) Logger.log(text);
}
The area between SELECT and FROM is the selector. You're not selecting any fields with that query. That's happening because the headerNames array is empty. Verify the value of REPORT_SHEET_NAME
I've taken on a new role in the tech support team and tasked to resolve an on going and intermittent issue with one of our nightly data extracts failing.
The extract process sends a data file every night containing a record of transactions completed that day. A scheduled task runs each night, with a second run if the first failed, then sends an email notification based on the status.
The scheduled task opens a CMD file which then runs a JS script command to execute a custom stored procedure on the SQL database.
Each night the file is replaced with a new file and this logic writes the status to file.
The below is the output log information which indicates an invalid call error:
Custom_Extract_Second_Run.js started at Tuesday, 19 January 2016 8:15:00 PM
C:\DataExtract\Custom_Extract_Second_Run.js(341, 4) Microsoft JScript runtime error: Invalid procedure call or argument
Custom_Extract.js started at Wednesday, 20 January 2016 8:03:00 PM
Script causes an exception.
Exception number: 0x5007
Description:'objRs.EOF' is null or not an object
Task ended at Wednesday, 20 January 2016 8:08:13 PM. Total Messages Sent: 0
Custom_Extract_Second_Run.js started at Wednesday, 20 January 2016 8:15:00 PM
Task ended at Wednesday, 20 January 2016 8:20:00 PM. Total Messages Sent: 1
Custom_Extract.js started at Thursday, 21 January 2016 8:03:01 PM
Script causes an exception.
Exception number: 0x5007
Description:'objRs.EOF' is null or not an object
Task ended at Thursday, 21 January 2016 8:07:56 PM. Total Messages Sent: 0
Custom_Extract_Second_Run.js started at Thursday, 21 January 2016 8:15:00 PM
Script causes an exception.
Exception number: 0x3704
Description:Operation is not allowed when the object is closed.
Task ended at Thursday, 21 January 2016 8:19:49 PM. Total Messages Sent: 0
Custom_Extract.js started at Friday, 22 January 2016 8:03:00 PM
C:\DataExtract\Custom_Extract.js(318, 4) Microsoft JScript runtime error: Invalid procedure call or argument
Custom_Extract_Second_Run.js started at Friday, 22 January 2016 8:15:00 PM
C:\DataExtract\Custom_Extract_Second_Run.js(341, 4) Microsoft JScript runtime error: Invalid procedure call or argument
We had to reverse-engineer the scripts used to work out how it actually behaves, however not at a confident level to make changes. The extract logic was developed a long time ago (10 to 12 years ago), therefore likely very old and outdated logic.
Below is the javascript logic used in the extract.
var QUERY_NAME_1 = "Custom_Extract";
var QUERY_NAME_2 = "Custom_Extract_UpdateDate";
var LOG_FILE_NAME = "DataExtract_error.log";
var EXTRACT_FILE_NAME = "Extract";
var DB_SERVER_NAME = "sql6";
var DB_DATABASE_NAME = "database_name";
var DB_DATABASE_USER_NAME = "username";
var DB_DATABASE_PASSWORD = "password";
var DateTimeNow = new Date();
var DATE_VALUE = DateTimeNow.getDate() + "/" + (DateTimeNow.getMonth() + 1) + "/" + DateTimeNow.getFullYear();
var EMAIL_SUBJECT_HEADER = "Email Extract Header - Extract Date - " + DATE_VALUE;
var EMAIL_SUBJECT = EMAIL_SUBJECT_HEADER + " Successful";
var EMAIL_SUBJECT_ERROR = "Unsuccessful - " + EMAIL_SUBJECT_HEADER;
var EmailCount = 0;
var DELIMITER = "|";
var STRING_SQL_FAILED = "SQL Failed";
var strScript = "";
var strMsg = "";
var ARG_REPORT_PATH = "";
var ARG_MAIL_SERVER = "";
var ARG_KEEP_BACKUP_COPIES = 0;
var ARG_EMAIL_FROM = "";
var ARG_EMAIL_TO = "";
function ParseCommandLineParams() {
var cmdArgs = WScript.Arguments;
for (i = 0; i < cmdArgs.length; i++) {
// Get next argument
arg = cmdArgs(i);
// Each argument is in the form a=b. Split the two components
j = arg.search(/=/);
if (j == -1) {
WScript.Echo("Invalid arguments arg = " + arg);
return false;
}
argName = arg.substring(0, j);
argValue = arg.substring(j + 1, arg.length);
switch (argName) {
case "/reportPath":
ARG_REPORT_PATH = argValue;
break;
case "/mailServer":
ARG_MAIL_SERVER = argValue;
break;
case "/keepBackupCopies":
ARG_KEEP_BACKUP_COPIES = parseInt(argValue);
break;
case "/from":
ARG_EMAIL_FROM = argValue;
break;
case "/to":
ARG_EMAIL_TO = argValue;
break;
default:
WScript.Echo("Invalid arguments. Name = '" + argName + "' Value = '" + argValue + "'");
return false;
}
}
return true;
}
function DisplayUsage() {
WScript.Echo("Usage: cscript DataExtract.js");
WScript.Echo(" [/m=<Report path directory where it creates file>]");
WScript.Echo(" [/from=<from-line in email>] [/to=<to-line in email>]");
WScript.Echo(" [/machinename=<Email Server name>] [/databaseConfig=<Database Config Setting file>]");
WScript.Echo(" ");
WScript.Echo(" /m - unc path to mail directory (default: ..\\reports\\)");
WScript.Echo(" /from - from-line in email (default: Tejas Magia <>");
WScript.Echo(" /to - to-line in email (default: Tejas Magia <>");
WScript.Echo(" /machinename - email server name (default: EXCHANGE2 ");
WScript.Echo(" /databaseConfig - DataBase Setup Config (default: ..\\..\\..\\..\\config\\config.txt ");
return;
}
function DisplayParameters() {
WScript.Echo("Parameters:");
WScript.Echo(" ARG_REPORT_PATH=[" + ARG_REPORT_PATH + "]");
WScript.Echo(" ARG_EMAIL_FROM=[" + ARG_EMAIL_FROM + "]");
WScript.Echo(" ARG_EMAIL_TO=[" + ARG_EMAIL_TO + "]");
WScript.Echo(" ARG_MAIL_SERVER=[" + ARG_MAIL_SERVER + "]");
WScript.Echo(" DB_SERVER_NAME=[" + DB_SERVER_NAME + "]");
WScript.Echo(" DB_DATABASE_NAME=[" + DB_DATABASE_NAME + "]");
return;
}
function GetConfigCDO() {
var objConf = WScript.CreateObject("CDO.Configuration");
objFields = objConf.Fields;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/sendusing") = 2;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ARG_MAIL_SERVER;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/smtpserverport") = 25;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30;
objFields.Update();
return objConf;
}
function GetDatabaseConnection() {
// SQLOLEDB is very sensitive to noise between client & server, and always mistaken certain
// noise as returned result set; ODBC is better way to go for now
var objCon = WScript.CreateObject("ADODB.Connection");
objCon.Open(
"Driver={Sql Server};APP=" + strScript +
";Server=" + DB_SERVER_NAME +
";Database=" + DB_DATABASE_NAME +
";user id=" + DB_DATABASE_USER_NAME +
";password=" + DB_DATABASE_PASSWORD +
";OLE DB Services=0"
);
return objCon;
}
//
// BEGIN MAIN PROGRAM
//
strMsg = "**************************************************";
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
strScript = String(WScript.ScriptName);
DateTimeNow = new Date();
strMsg = strScript + " started at " + DateTimeNow.toLocaleString();
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
if (!ParseCommandLineParams()) {
DisplayUsage();
WScript.Quit(0);
}
DisplayParameters();
try {
var objCon = GetDatabaseConnection();
var objCmd = WScript.CreateObject("ADODB.Command");
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_1;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
var objParam = objCmd.CreateParameter("Return", 3, 4); // adInteger, adParamReturnValue
objCmd.Parameters.Append(objParam);
WScript.Echo("Before execution");
if (ARG_KEEP_BACKUP_COPIES == 1) {
var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt")) {
var File3 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
File3.Delete();
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt")) {
var File2 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
File2.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt")) {
var File1 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
File1.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt")) {
var File0 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt");
File0.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
}
}
var objRs = null;
try {
objRs = objCmd.Execute();
} catch (er) {
try {
objRs = objCmd.Execute();
} catch (er2) {
try {
objRs.Close();
} catch (er3) {
WScript.Echo("Error found even after 2nd run attempt!");
}
var strError = "First run failed";
// Send error email
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
// objMsg.To = ARG_EMAIL_TO;
objMsg.To = "support#sumtotal.com.au";
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT_ERROR;
objMsg.TextBody = strError;
objMsg.send();
// Log error in output file
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
objTStreamLE.WriteLine(strError);
objTStreamLE.Close();
}
}
if (objRs.EOF) {
objRs.Close();
// check return code after closing the record set
var rc = objCmd(0);
if (rc == 100010) {
WScript.Echo("Another instance is running. Bail out");
} else if (rc == 0) {
WScript.Echo("Found no records for users");
} else {
var objError = new Error(rc, STRING_SQL_FAILED);
throw objError;
}
} else {
// Update Last Update Date
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_2;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
objCmd.Execute();
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
while (true) {
if (objRs.EOF) {
// we have to send the last mail
strContentType = "";
} else {
var strApplicantID = new String(objRs("EmpID"));
var strCourse_Code = new String(objRs("Event_Code"));
var strCourse_Name = new String(objRs("Course_Name"));
var strCompletion_Date = new String(objRs("Completion_Date"));
var strGrade_Name = new String(objRs("Grade_Name"));
var strBodyInfo = strApplicantID + DELIMITER +
strCourse_Code + DELIMITER +
strCourse_Name + DELIMITER +
strCompletion_Date + DELIMITER +
strGrade_Name;
WScript.Echo(strBodyInfo);
objTStreamLE.WriteLine(strBodyInfo);
}
if (objRs.EOF) {
break;
}
objRs.MoveNext;
}
objTStreamLE.Close();
objRs.Close();
}
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT;
objMsg.TextBody = EMAIL_SUBJECT;
objMsg.send();
EmailCount++; // How many email sent. it will be always one email in this case.
objCon.Close();
} catch (e) {
var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
var objTStreamErrorLog = objFSO.CreateTextFile(errorFile, true);
objTStreamErrorLog.WriteLine(strMsg);
if (e.description == STRING_SQL_FAILED) {
strMsg = "Failed to execute " + QUERY_NAME_1 + ".\nReturn code:" + String(e.number);
} else {
strMsg = "Script causes an exception.\nException number: 0x" + String(e.number & 0xFFFF) + "\nDescription:" + e.description;
}
objTStreamErrorLog.WriteLine(strMsg);
objTStreamErrorLog.close();
var errorFile = ARG_REPORT_PATH + LOG_FILE_NAME;
var theFile_1_Abs = objFSO.GetAbsolutePathName(errorFile);
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO + ";" + ARG_EMAIL_FROM;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT_ERROR;
objMsg.TextBody = "The attached file contains a error log file of any issues that arose during the DataExtract process";
objMsg.AddAttachment(theFile_1_Abs);
objMsg.send();
WScript.StdErr.WriteLine(strMsg);
WScript.Echo(strMsg);
objFSO.DeleteFile(errorFile); // delete log file generated.
var objShell = WScript.CreateObject("WScript.Shell");
if (!objShell.LogEvent(1, "iX Learning Server Task(" + strScript + "): " + strMsg)) {
WScript.StdErr.WriteLine("Failed to log an event");
}
}
DateTimeNow = new Date();
strMsg = "Task ended at " + DateTimeNow.toLocaleString() + ". Total Messages Sent: " + EmailCount.toString();
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
//
// END MAIN PROGRAM
//
2nd run
var QUERY_NAME_1 = "Custom_Extract_Second_Run";
var QUERY_NAME_2 = "Custom_Extract_UpdateDate";
var QUERY_NAME_3 = "Custom_Extract_GetRunStatus";
var LOG_FILE_NAME = "DataExtract_error.log";
var EXTRACT_FILE_NAME = "Extract";
var DB_SERVER_NAME = "sql6";
var DB_DATABASE_NAME = "database_name";
var DB_DATABASE_USER_NAME = "username";
var DB_DATABASE_PASSWORD = "password";
var DateTimeNow = new Date();
var DATE_VALUE = DateTimeNow.getDate() + "/" + (DateTimeNow.getMonth()+1) + "/" + DateTimeNow.getFullYear();
var EMAIL_SUBJECT_HEADER = "Email Extract Header - Extract Date - " + DATE_VALUE;
var EMAIL_SUBJECT = EMAIL_SUBJECT_HEADER + " Successful";
var EMAIL_SUBJECT_ERROR = "Unsuccessful - " + EMAIL_SUBJECT_HEADER;
var EmailCount = 0;
var DELIMITER = "|";
var STRING_SQL_FAILED = "SQL Failed";
var strScript = "";
var strMsg = "";
var ARG_REPORT_PATH = "";
var ARG_MAIL_SERVER = "";
var ARG_KEEP_BACKUP_COPIES = 0;
var ARG_EMAIL_FROM = "";
var ARG_EMAIL_TO = "";
function ParseCommandLineParams()
{
var cmdArgs = WScript.Arguments;
for(i = 0; i < cmdArgs.length; i++)
{
// Get next argument
arg = cmdArgs(i);
// Each argument is in the form a=b. Split the two components
j = arg.search(/=/);
if(j == -1)
{
WScript.Echo("Invalid arguments arg = " + arg);
return false;
}
argName = arg.substring(0, j);
argValue = arg.substring(j+1, arg.length);
switch(argName)
{
case "/reportPath":
ARG_REPORT_PATH = argValue;
break;
case "/mailServer":
ARG_MAIL_SERVER = argValue;
break;
case "/keepBackupCopies":
ARG_KEEP_BACKUP_COPIES = parseInt(argValue);
break;
case "/from":
ARG_EMAIL_FROM = argValue;
break;
case "/to":
ARG_EMAIL_TO = argValue;
break;
default:
WScript.Echo("Invalid arguments. Name = '" + argName + "' Value = '" + argValue + "'");
return false;
}
}
return true;
}
function DisplayUsage()
{
WScript.Echo("Usage: cscript DataExtract.js");
WScript.Echo(" [/m=<Report path directory where it creates file>]");
WScript.Echo(" [/from=<from-line in email>] [/to=<to-line in email>]");
WScript.Echo(" [/machinename=<Email Server name>] [/databaseConfig=<Database Config Setting file>]");
WScript.Echo(" ");
WScript.Echo(" /m - unc path to mail directory (default: ..\\reports\\)");
WScript.Echo(" /from - from-line in email (default: Tejas Magia <>");
WScript.Echo(" /to - to-line in email (default: Tejas Magia <>");
WScript.Echo(" /machinename - email server name (default: EXCHANGE2 ");
WScript.Echo(" /databaseConfig - DataBase Setup Config (default: ..\\..\\..\\..\\config\\config.txt ");
return;
}
function DisplayParameters()
{
WScript.Echo("Parameters:");
WScript.Echo(" ARG_REPORT_PATH=[" + ARG_REPORT_PATH + "]");
WScript.Echo(" ARG_EMAIL_FROM=[" + ARG_EMAIL_FROM + "]");
WScript.Echo(" ARG_EMAIL_TO=[" + ARG_EMAIL_TO +"]");
WScript.Echo(" ARG_MAIL_SERVER=["+ ARG_MAIL_SERVER +"]");
WScript.Echo(" DB_SERVER_NAME=[" + DB_SERVER_NAME + "]");
WScript.Echo(" DB_DATABASE_NAME=[" + DB_DATABASE_NAME+ "]");
return;
}
function GetConfigCDO()
{
var objConf = WScript.CreateObject("CDO.Configuration");
objFields = objConf.Fields;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/sendusing") = 2;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ARG_MAIL_SERVER;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/smtpserverport") = 25;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30;
objFields.Update();
return objConf;
}
function GetDatabaseConnection()
{
// SQLOLEDB is very sensitive to noise between client & server, and always mistaken certain
// noise as returned result set; ODBC is better way to go for now
var objCon = WScript.CreateObject("ADODB.Connection");
objCon.Open(
"Driver={Sql Server};APP=" + strScript +
";Server=" + DB_SERVER_NAME +
";Database=" + DB_DATABASE_NAME +
";user id=" + DB_DATABASE_USER_NAME +
";password=" + DB_DATABASE_PASSWORD +
";OLE DB Services=0"
);
return objCon;
}
//
// BEGIN MAIN PROGRAM
//
strMsg = "**************************************************";
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
strScript = String(WScript.ScriptName);
DateTimeNow = new Date();
strMsg = strScript + " started at " + DateTimeNow.toLocaleString();
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
if (!ParseCommandLineParams())
{
DisplayUsage();
WScript.Quit(0);
}
DisplayParameters();
try
{
var objCon = GetDatabaseConnection();
var objCmd = WScript.CreateObject("ADODB.Command");
var objRs = null;
var runStatus = 0;
try
{
// Get Run Status
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_3;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
objRs = objCmd.Execute();
runStatus = objRs("Status");
}
catch (er5)
{
}
if (runStatus < 1)
{
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_1;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
var objParam = objCmd.CreateParameter("Return", 3, 4); // adInteger, adParamReturnValue
objCmd.Parameters.Append(objParam);
WScript.Echo("Before execution");
if (ARG_KEEP_BACKUP_COPIES == 1)
{
var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt"))
{
var File3 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
File3.Delete();
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt"))
{
var File2 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
File2.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt"))
{
var File1 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
File1.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt"))
{
var File0 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt");
File0.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
}
}
try
{
objRs = objCmd.Execute();
}
catch (er)
{
try
{
objRs = objCmd.Execute();
}
catch (er2)
{
try
{
objRs.Close();
}
catch (er3)
{
WScript.Echo("Error found even after 2nd run attempt!");
}
var strError = "An unexpected error has occured during the run of this routine. " +
"and send through a manual extract. If you do not hear back from us by 1pm, please do not hesitate to call " +
".";
// Send error email
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO + ";email#email.com.au";
// objMsg.To = ARG_EMAIL_TO;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT_ERROR;
objMsg.TextBody = strError;
objMsg.send();
// Log error in output file
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
objTStreamLE.WriteLine(strError);
objTStreamLE.Close();
}
}
if (objRs.EOF)
{
objRs.Close();
// check return code after closing the record set
var rc = objCmd(0);
if (rc == 100010)
{
WScript.Echo("Another instance is running. Bail out");
}
else if (rc == 0)
{
WScript.Echo("Found no records for users");
}
else
{
var objError = new Error(rc, STRING_SQL_FAILED);
throw objError;
}
}
else
{
// Update Last Update Date
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_2;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
objCmd.Execute();
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
while (true)
{
if (objRs.EOF)
{
// we have to send the last mail
strContentType = "";
}
else
{
var strApplicantID = new String(objRs("EmpID"));
var strCourse_Code = new String(objRs("Event_Code"));
var strCourse_Name = new String(objRs("Course_Name"));
var strCompletion_Date = new String(objRs("Completion_Date"));
var strGrade_Name = new String(objRs("Grade_Name"));
var strBodyInfo = strApplicantID + DELIMITER +
strCourse_Code + DELIMITER +
strCourse_Name + DELIMITER +
strCompletion_Date + DELIMITER +
strGrade_Name;
WScript.Echo(strBodyInfo);
objTStreamLE.WriteLine(strBodyInfo);
}
if (objRs.EOF)
{
break;
}
objRs.MoveNext;
}
objTStreamLE.Close();
objRs.Close();
}
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT;
objMsg.TextBody = EMAIL_SUBJECT;
objMsg.send();
EmailCount++; // How many email sent. it will be always one email in this case.
}
objCon.Close();
}
catch (e)
Following a great deal of testing and analysis, we're unable to pinpoint the root cause as both jobs seem to fail intermittently, some nights the extract produces data and other nights no data at all.
Below are our test results based on manual run via cmd and via the task both producing intermittent failed and success results, with no particular pattern.
Test results:
Execute 1st run via command prompt
1st run - Fail
2nd run - Fail
3rd run - Successful
4th run - Successful
5th run - Successful
Execute Scheduled task - 'First run'
1st run - Successful
2nd run - Successful
3rd run - Successful
4th run - Fail - Scheduler failed
5th run - Fail - Scheduler failed
Execute Scheduled task - Sequential First followed by Second run
1st run - First run successful, second run successful but no file generated
2nd run - First run unsuccessful, ran second job - successful
3rd run - First run successful, second run successful but no file generated
4th run - First run unsuccessful, ran second job - successful
A possible cause may be due to timeout; however, each run takes the same time to complete (approx. 3 minutes) whether failed or successful.
We recently upgraded the environment from Windows Server 2005 to Windows Server 2008 and noticed the issue starting occurring after implementation. No change was made to the logic or SQL procedures.
Would Server 2008 handle the logic in such a way to break the routine?