Windows Gadget reading specific information from Excel with Javascript - javascript

Hi I am trying to write a Windows Gadget that reads out information from an Excelsheet.
The Excelsheet entails a the dates, formated, of the whole year in column A2:A366 and the following columns are the names of the employes B1:Q1.
The gadget is to display the current day and who is marked absent. For each day a person is absent the cell is marked with an X.
I am not a Javascript programmer. And need help. I think I have the basic setup already, and I hope you can help me find my missings.
Explanation:
With the getToday function I am trying to get the date from the PC and format it to a string which I want to use to find in the Column A, which is set to be an array. The same function ist ment to give me back the right row in which it should look for the Xs. If it finds an X it is supposed to return the name of the column i.e the name of the employee.
function getToday (){
var today;
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
today = d.toString (day + "." + month + "." + year);
}
function refreshData(){
oExcel = new ActiveXObject('Excel.Application');
oWkBooks = oExcel.Workbooks.Open("C:\\Program Files\\Windows Sidebar\\Gadgets\\ExcelGadget.Gadget\\test.xlsx");
oExcelSheet = oWkBooks.Worksheets();
oExcelSheet.Activate();
oExcel.ActiveWorkbook.RefreshAll();
oExcel.ActiveWorkbook.SaveAs("C:\\Program Files\\Windows Sidebar\\Gadgets\\ExcelGadget.Gadget\\test.xlsx");
oWkBooks.Close();
location.reload();
}
function fetchData() {
function fetchData() {
$('#msg').html("Loading...");
$('#msg').show();
var oExcel;
var oExcelSheet;
var oWkBooks;
var cols;
oExcel = new ActiveXObject('Excel.Application');
oWkBooks = oExcel.Workbooks.Open("C:\\Program Files\\Windows Sidebar\\Gadgets\\ExcelGadget.Gadget\\test.xlsx");
}
function findToday(stringArray){
for (var j=0; j<stringArray.length; j++) {
if (stringArray[j].match (var today) return cell;
return -1;
}
function returnAbwesentheit() {
var name = name.arr;
for (i=2;i<x.length;i==23) {
if ("cell"=="x") {
document.write (Name(cell));
else
return null;
}
}
::UPDATE::
I had a flash of inspiration. I think I am making this to difficult for myself. Maybe I could make excel do the finding of the date and who is absent. Then I would only generate the Outcome with Javascript into the Windows Gadget.

Related

why do i get an error when running my program: "dates is not defined," when dates is defined in an external js library?

I am coding a program with a dropdown menu i imported from a library. my objective is to obtain the day and month and use it to compute the zodiac sign. later, i will have an array of random horoscopes and that will be displayed as well. however, after i execute my code, i get an error: "dates is not defined." how can i reference the date and month from the dropdown menu correctly?
var astro_sign
function getzodiac() {
var day = parseInt(document.getElementsByClassName('bear-dates').value);
var month = document.getElementsByClassName('bear-months').value;
var year = document.getElementsByClassName('bear-years').value;
if (month == "December"){
if (day < 22)
astro_sign = "Sagittarius";
else
astro_sign ="Capricorn";
}
and then I want it to print an output:
document.getElementById("zodiac").innerHTML="You were born under the sign of" + " "+astro_sign+"!"
here is my body:
<h5 id="zodiac"> </h5>
<button class="button" onclick="getzodiac()">get my zodiac!</button>
This is how dates is defined in the library js file:
function dates(tags)
{
if(tags == '')
//If the dates('') paramenter is empty, add no tags
{
var dates = "";
var i;
for (i = 1; i < 32; i++ )
{
dates += i;
}
}
else
//If the dates('option') has paramenter, add the tags to it
{
var dates = "";
var i;
for (i = 1; i < 32; i++ )
{
dates += "<" + tags +">" + i +"</" + tags +">";
}
}
In the code inserted in HTML, you have a reference to dates() function which is declared in the calendar.js file but you have not exported it using 'export' keyword. MDN export
export dates(){
//some code
}
next you can import it in HTML code script
import dates from './calendar.js'
now you can use the function dates() in your code.
the same goes for the other functions declared in calendar.js, months() and years()

Concatenating strings in Google Apps Script

How do I get both values represented by i and j into the getSheetByName function?
Disclaimer: I am brand new at coding and am probably asking the wrong questions. My goal is to create a simple code that will delete sheets automatically by looping through the sheet names: Week 1, Week 2, etc.
Here's my code so far:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var i = "Week "
var j = 32
var mysheet = sheet.getSheetByName(i&j)
sheet.deleteSheet(mysheet)
}
In your code you have written i&j that's not the syntax to concat in Google apps script. Instead you can simply use i + j.
For looping you'll need to use any loop, like for, while, do-while.
Combining these suggestions, here is my final suggestion.
Try something like this [By using 'try' here I mean, simply don't copy-n-paste and use. Try to understand and write your own code, curated for your specific need. Maybe that way, we'll grow/learn more]
function myFunction() {
var START_WEEK = 1; //Put your starting week count here
var END_WEEK = 2; //Put your ending week count here
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
var pre_name = "Week"
var i;
for (i = START_WEEK; i <= END_WEEK; i++) {
try {
spreadSheet.deleteSheet(spreadSheet.getSheetByName(pre_name + " " + i))
} catch (exp) {
//Maybe sheet with that name is not found
Logger.log(exp.toString());
}
}
}
This code loop through all sheet whose name start with "Week " and then followed by week count, up till the end week count and if that's found it's deleting them.
Make sure you put in START_WEEK and END_WEEK properly.
Let me know if this doesn't work for you or if you have any query.
Thanks

using the HolidayAPI for bank holidays

the Question:
How can I use the API to return a boolean value if the date is a bank holiday?
I have done some research and found a great, and free API which contains bank holidays, however I am having trouble using it: http://holidayapi.com/
if i was to use this code:
var year = 2016;
var month = 3;
var day = 25;
var isAHoliday = false;
$.getJSON(
"http://holidayapi.com/v1/holidays?country=GB&year=" + year + "&month=" + month + "&day=" + day, function (data) {
console.log(data); //DOES NOT DISPLAY IN CONSOLE
if (data.holidays.length > 0) {
// BANK HOLIDAY
isAHoliday = true;
}
else {
//IS NOT BANK HOLIDAY
//AND NOTHING NEEDS TO BE DONE
}
});
i want to be able to return a true or false value depending on if this returns any data or not, however im doing something wrong as the getJSON request is not being called, please could someone correct me where i have gone wrong?
http://holidayapi.com/v1/holidays?country=GB&year=2016&month=03&day=25 returns {"status":200,"holidays":[{"name":"Good Friday","country":"GB","date":"2016-03-25"}]}
http://holidayapi.com/v1/holidays?country=GB&year=2016&month=03&day=26 returns {"status":200,"holidays":[]}
it appears this is causing an issue: "http://holidayapi.com/v1/holidays?country=GB&year=" + year + "&month=" + month + "&day=" + day; if i pass one of the 2 URL's in above i get the correct result, I am having a play now with this
https://jsfiddle.net/dcxk6ens/
If you simply want to return a true value if the selected date is a holiday, or false if it is not, you could use a function like this:
(Please note that jsfiddle will not execute any AJAX calls to URLs using the "http://" protocol, since it is not secure.)
function isDateAHoliday(y, m, d) {
var jsonURL = "http://holidayapi.com/v1/holidays?country=GB&year=" + y + "&month=" + m + "&day=" + d;
var isAHoliday = false;
$.getJSON(jsonURL, function (data) {
// If the date is a holiday
if (data.holidays.length > 0) {
// Do some things
isAHoliday = true;
}
// Check values
console.log("JSON DATA: ", data);
console.log("Holiday?: " + isAHoliday);
return isAHoliday;
});
}
isDateAHoliday("2016", "3", "25");
If you wanted to return the name and country of the holiday as well, you could substitute isAHoliday = data.holidays[0]; inside of the if statement.
The holidays object must be called as a child of the returned data object:
Since the holidays object is an array you'll also need to use an index to access an item. Assuming there is at least one item returned, you would get the date like so:
var myDate = data.holidays[0].date;
However you should always check that there's at least one object in the array before getting the first one:
if(data.holidays.length > 0){...}
Incidentally, if all you want to do is check if there's a holiday on any particular day then this if statement is all you'll need, since an array length of more than zero means there's at least one holiday.
Edit
A full answer to your question, you could put this inside the .done() method:
var isAHoliday = false;
if(data.holidays.length > 0){
// There's at least one holiday today!
isAHoliday = true;
}
You don't have to declare a local variable, you'll probably use one that's declared elsewhere but that's up to you.

InDesign script to pull file by week number

I have a script for our newspaper that pulls comics from a folder and places them in an InDesign document. Most comics have the full date in the filename, but a few Sunday comics go by the week number for that Sunday. I.E. Blondie sunday filenames are like bln04ts.pdf or bln05ts.pdf. When we run the script we select the day to pull comics for but I'm unsure how to pull these files by week number?
Here is a sample by full date in filename if that helps at all.
` // LUANN
if (pageItem.label == "LUANN")
try{
name = "lu" + myDate.text + ".tif"
var myFile = new File(imagePath+name);
if (myFile.exists)
{
pageItem.place(myFile);
pageItem.fit(FitOptions.CONTENT_TO_FRAME);
}
name = "lu" + myDate.text + "_vacation.tif"
var myFile = new File(imagePath+name);
if (myFile.exists)
{
pageItem.place(File(myFile));
pageItem.fit(FitOptions.CONTENT_TO_FRAME);
}
name = "lu" + myDate.text + "_crx.tif"
var myFile = new File(imagePath+name);
if (myFile.exists)
{
pageItem.place(File(myFile));
pageItem.fit(FitOptions.CONTENT_TO_FRAME);
}
}catch(err){
//alert("caught error")
}`
Any ideas?
Well, if I understand you correctly you would need to add some lines like these to your script:
name = "bln" + myDate.week + "ts.pdf";
var myFile = new File(imagePath+name);
if (myFile.exists)
{
pageItem.place(File(myFile));
pageItem.fit(FitOptions.CONTENT_TO_FRAME);
}
However you'll also need to change the setup of you myDate object to include a week property. I don't know how the myDate object is set up (does it create the date text from the current date? Or from the file name of the document? Or something else?), so you would need to post a snippet of that, if you need further assistance on that step.

Combine a Date & a Time Column in Google Sheets for a createEvent() function [duplicate]

This question already has an answer here:
How to merge date and time as a datetime in Google Apps Spreadsheet script?
(1 answer)
Closed 7 years ago.
I need to combine two columns into one dateTime column that can be reading by a createEvent function for startDate.
Column F is the date (mm-dd-yyyy) & Column G is the time (HH:mm PM/AM). I am currently combine them in Column H with the following code:
function conCat() {
var sheet = SpreadsheetApp.getActiveSheet();
var numberOfRows = sheet.getLastRow().toString();
var range = sheet.getRange(2,1,numberOfRows,12);
var values = range.getValues();
var consultedAlreadyFlag = sheet.getRange(2,10,numberOfRows,12);
var sheetName = sheet.getSheetName();
//show updating message
consultedAlreadyFlag.setFontColor('red');
var numValues = 0;
for (var row = 2; row < values.length.toString(); row++) {
//check to see if name and type are filled out - date is left off because length is "undefined"
if (values[row][3].length > 0) {
currentStatus = values[row][1];
//check if it's been entered before
if (values[row][9] != 'EMAIL_SENT'){
sheet.getRange(row+2,8,1,1).setValue('=F' +(row+2)+ '+G' +(row+2));
}
else{
//sheet.getRange(row+2,10,1,1).setValue('EMAIL_SENT');
}
numValues++;
}
//hide updating message
consultedAlreadyFlag.setFontColor('green');
}
}
This code isn't working because when someone submits the form, and the code combines the columns, I cannot get the format to come out as "mm-dd-yyyy HH:mm:ss" which I feel I need in order for my createEvent function to work.
How can I get it to combine the two columns to get the format I need?
I wouldn't bother trying to combine the two columns. There's no point in adding another column, I don't think. You can use JavaScript methods like setHours():
function fncAddToCalender() {
var theEventDate = //To Do - Get the Date
var startMin = value from sheet cell
theEventDate.setHours(15); //Sets event date to 3pm
theEventDate.setMinutes(startMin);
var cal = CalendarApp.getDefaultCalendar();
var event = cal.createEvent(calndrTitle, theEventDate, endDate, {
description : theDescrptn,
location : theLocation,
guests : guestToInvite,
sendInvites : true
});
};
The format in the spreadsheet is probably set to "date". And getting the values with Apps Script will probably return a value that's already in a date format. If you have the columns formatted as text, you'd need to change the values to a date.

Categories