I'm working on a spreadsheet in Google Drive to keep track of when we need to get our employees their annual reviews. As part of the calculations, I need to be able to compute their anniversary dates by adding years to their start dates. I'm trying this:
returnDate = returnDate.setYear( returnDate.getYear() + 1);
Alas, the scripting language that google supports seems to lack many of the useful functions of a fully-fledged Javascript Date object, as I get this error on that line when I try to run it:
TypeError: Cannot find function setYear in object 1357106400000. (line 28, file "Code")
So, how can one compute a series of anniversary dates using the tools available in Google Apps Script?
Thanks for any help.
Works like this :
function addOneYear(y){
var fullYear = y.getFullYear();
return new Date(y.setFullYear(fullYear+1));
}
According to Javascript specifications here getYear and setYear are not recommended
Related
I am running Google's Analytics Add-on for Google Sheets, for Chrome, to pull in analytics for several properties. This runs once a day and works very well. Two data points that I pull are the approximate latitude and longitude of the visitor.
Prior to using the add-on I used the older "magic script" written by Nick Mihailovski however this doesnt work any longer so we're advised to the add-on.
What I would like to do is extend the add-on such that after it populates the sheets I would like to add a column that shows the reverse geocode of the coordinates.
I have this function that I used to modify Nick's script:
function reverse_geocode(lat,lng) {
Utilities.sleep(1500);
var response = Maps.newGeocoder().reverseGeocode(lat,lng);
for (var i = 0; i < response.results.length; i++) {
var result = response.results[i];
Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
result.geometry.location.lng);
return result.formatted_address;
}
}
I was able to modify Nick's code so that as each row was written to the sheet I could add the cell with the address from the lat/long. Now I must do this manually. I'd like to get it back to working automatically.
Is it possible to do this with an add-on for which I can not see nor access the code? I have tried to add this function to a file in my sheet called "geocode.gs" and tried to call it via a trigger but it does nothing. No visible error that I can see, nothing in the execution log either. Is there another way to automate this with a closed-source add-on?
You can do this by creating a bound script in the Sheet you are running the report on.
If you've already tried and it doesn't work it may just be due to an error in your code.
I don't think there are add-ons that can do what you want specifically, also because doing it with the code in Google Apps Script (since you already have a function that does its job) is the quickest and most effective way.
Can googlefinance write results directly to an array? I am currently writing it to a sheet first and then pulling the sheet range into the array. It would save a lot of processing if I could write it directly to an array so I am investigating. If you have knowledge and expertise on this could you let me know?
I get an error when I try. Is it just incorrect syntax or is what I am trying to do not possible? I would like to avoid writing to the sheet to save on time and in sheet processing. But not sure if the function is allowed to write into a variable instead of a sheet
function TEST() {
var APPLEPRICEARRAY = GOOGLEFINANCE("AAPL","price","1/1/2009","12/31/2020","WEEKLY")
};
Is it just incorrect syntax?
You seem to confuse functions (formulas) that are exposed in the Google Sheets application with services that are available in Google Apps Script environment. What you wrote would require GOOGLEFINANCE to be a service "attached" to global scope, so yes, this is incorrect.
But the error you get is not a syntax error, your reference is invalid (no GOOGLEFINANCE variable is ever declared in the global scope, therefore none can be referenced), hence you get ReferenceError.
Can googlefinance write results directly to an array?
No, for reasons explained above, it cannot. I apologize for this, but you are comparing apples with oranges: an array is a data structure (an indexed collection, to be precise) in JavaScript (on which Google Apps Script language is based), while formulas are not even built-in objects: they are not part of the language.
Is what I am trying to do not possible?
Unfortunately, Google Finance API has been shut down for a long time now, so no, not possible.
Not screaming with ALL-CAPS is considered a common courtesy as well
In the code example is shown that you are trying to use a Google Sheets function as a Google Apps Script / JavaScript function. That is not possible.
Unfortunately there isn't Google Apps Script advanced service for Google Finance and there isn't a API.
From https://groups.google.com/g/google-finance-apis
Dear Finance API users,
As you may have heard from the Google Developers Blog, Google is doing an API spring cleaning. One of the APIs affected is the Google Finance API (both the Portfolio API and the Finance Gadgets and Tools API), which will be shut down on October 20, 2012.
References
https://developers.google.com/apps-script/guides/services/advanced
I'm using Google app scripts to write data to Google Sheets with custom calculations which is why I'm using scripts instead of a straight export of the data. When the data is written into Google Sheets, I want it sorted by Cost/Conv or CPA.
The current script sorts by Conversions:
var kw=AdWordsApp.keywords().withCondition("Conversions >1").withCondition("Status =ENABLED").forDateRange("20180101", "20180801").orderBy("Conversions DESC").withLimit(100).get();
How do I sort by conversion cost or cpa?
I looked through Googles documentation and even called their support team. Couldn't find the answer. I feel like this should be simple but I can't figure it out.
Apps Script seems to be unable to deal with a cell that uses the average formula, when the average is over Google Finance data:
Consider that column A is full of data from Google Finance. Then you type into cell B1 =average(A1:A100). Lets assume the average is 5 and as such cell, B1 shows 5. Great, except that using the following script will fail:
var AveResult = spreadsheet.getRange('B1').getValue();
----> The log show a #DIV/0! error
On the other hand, if you overwrite the formula in B1 and simply type in a 5, then it works perfectly:
var AveResult = spreadsheet.getRange('B1').getValue();
----> The log shows 5
Is there any way to get around this problem, to use scripts with summaries of Google Finance data?
Thank you.
As written in the Official documentation,
Historical data cannot be downloaded or accessed via the Sheets API or Apps Script. If you attempt to do so, you will see a #N/A error in place of the values in the corresponding cells of your spreadsheet.
The average formula is NOT the problem. Any attempt to take historical finance data through any formula hoops doesn't seem to be possible.
However live data doesn't seem to be restricted the same way.
How can you set the current time with google app scripts? I'm having a tough time finding the Time documentation.
I'm able to get the time in milliseconds using.
(new Date()).getTime()
I'd like to now format this time as 11:56 AM. I'm using the Date documentation. I've tried using toLocaleTimeString() from javascript but it is not defined in app scripts
For formatting a date, what you want is the Utilities.formatDate() function, documented here: https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
Usage to get the time only would be:
Utilities.formatDate(YOUR DATE,YOUR TIMEZONE, 'HH:mm')
replacing YOUR DATE and YOUR TIMEZONE with the appropriate values.