Google Analytics: Spreadsheet Add-on invalid value 'daysAgo' - google-apps-script

I'm trying to do build an automatic report query with the Google Analytics spreadsheet add-on. I want to get a monthly report (start of month to end of month) about users, visits etc. According to the Google Analytics documentation, it's possible to set a startDate and endDate in the following format:
Values must match [0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo).
However i'm getting an error which looks like this:
Report failed
My settings looks like this:
Settings in Google Spreadsheet
Does anybody know what causes my error? This feature will be really handy.
Thanks in advance.

First you have to schedule your report every month and then you can use the function eomonth() to calculate start and end dates.
Start date -> = EOMONTH(TODAY(),-2) +1
End date -> = EOMONTH(TODAY(),-1)

Try running it on a different profile or a larger date range. I was able to get data using the exact query that you shared. You can also try the Google Analytics Query Explorerand see if that gives you works or it gives you more detailed error information.

Related

Adding Date+Time without 12AM moving to the next day

I am using the attached to automate a time entry to go along with the date for a calendar import. The entries don't have times, and the staff will not enter them try as I might. I need to automate them to simplify the entry procedure.
The issue I am facing is that the Calendar API needs the data to be in DATE/TIME format. To do this I need to use the =DATE+TIME formula. When I do so and the time reaches 12:00AM, the dates thereafter change to the following day.
Essentially I need to either override the logic that makes it move into the next day after midnight appears, or I need to tell either the function in column B-C that it can never roll to midnight. I am trying to think of perhaps a way that I can tell the function to reset the time if the date in column A changes to a new day, and if it doesn't change to a new day go ahead and use the existing function and add 5 minutes to the time that is shown previously to it.
I am stumped, any help would be greatly appreciated.
Here is a sheet to show you the issue
Here is the formula I tried, which worked to sort out the problem but did not work with the Calendar API requirements to format at DATE/TIME. Even when using the importrange formula to move the data into a new sheet with the cells formatted as DATE/TIME it still recognizes it as TEXT as this is what the formula prescribes.
=IF(A2<>"",(CONCATENATE(TEXT(A2,"MM/DD/YYYY")&" "&TEXT(B2,"HH:MM:SS"))),"")
I need this to work in both the sheet and in the import to Calendar using the Calendar API requirements through APPScript.
If I understood correctly your question, here a suggestion with a custom Apps Script function called like a normal Google Sheet function.
Open Apps Script Editor and paste the function below
Call the function rebuildDateTime(COL1, COL2) inside your spreadsheet
Spreadsheet:
Code:
function rebuildDateTime(arg0, arg1) {
var date = new Date(arg0);
var str = arg1.toString().split(':');
date.setHours(str[0]);
date.setMinutes(str[1]);
return date;
}
Warning :
Your COL2 (which contains only the time), must be forced to TEXT FORMAT
References :
Create a custom function

Updating Google Sheet with WooCommerce Order

I need to be able to automatically update a google sheet file every time an order is placed through WooCommerce.
I've found the solution below, but using this each individual item ordered is listed as a new row. I'd like the order to be grouped under the order number and the item quantities separated into appropriate columns instead.
https://www.tychesoftwares.com/export-woocommerce-orders-to-google-sheets-in-realtime/
Below is a Google Sheet we are manually updating at present to show you what i mean.
Example
Is there a way to send the WooCommerce orders directly through to Google Sheets in this format?
Thanks so much in advance for any advice!
Yes it looks possible.
I know nothing about WooCommerce, but I believe you can sort out the received data in any way you want.
Look, the last line in their script appends the received data as a new row:
sheet.appendRow([timestamp,order_number,order_created,order_status]);
As far as I can see, the data contains the four elements:
timestamp
order_number
order_created
orders_status
Instead, you can put these elements into any cell on your table. Something like this, for example:
var ss = Spreadsheet.GetActiveSheet();
ss.getRange('A10').setValue(timestamp); // timestamp goes to A10
ss.getRange('B20').setValue(order_number); // order_number goes to B20
ss.getRange('C30').setValue(order_created + order_status); // created + status go to C30
The same way you can add any of these elements to some existing value in some cell, etc. For example:
var old_value = ss.getRange('A2').getValue(); // get value from the cell A2
var new_value = old_value + order_number; // add with order_number
ss.getRange('A2').setValue(new_value); // put the sum back into the cell A2
The main problem is up to you. You have to figure out:
what exactly the elements you're receiving (number, names)
how exactly you want to sort them out (what to add to what... what to put where... etc)
I can't understand it from the example picture.
Here is some reference documentation on Apps Script:
Main Page - Introducing Apps Script.
Sheets Guide - Introduction to Sheets with Apps Script.
Sheets Reference - Where you will find all the details of everything you can do with Sheets in Apps Script.
Remove Duplicate Rows - A good small tutorial that will teach you the basics of Sheets and Ranges and how to manipulate them.
To export all my WooCommerce orders on a scheduled basis, I used a ready-made solution.
I used a WooCommerce API and JSON client. It worked smoothly: I got the WooCommerce API, and the JSON client was implemented in the tool already.
You just need to choose endpoint in the JSON client to get the required data. I exported all orders once a month, so I used the base URL http:// mydomain /wp-json/wc/v3/orders and my endpoint was orders.
You can check this article to understand better how it works for your purpose.
And here is WooCommerce API documentation.
I assume that setting up an export through the Apps Script is more flexible (and based on the answer above, it's working indeed), but I'm not a code guy. So I searched for an easier solution, and the API + JSON client helped.
Hope you'll find it helpful.
I would like to suggest using WooCommerce Google Sheet Plugin

How to get the number of the current day of the year in google apps script?

Using google app script, I am accessing a google sheet of a rota with one sheet per year, with a row per day, with a column for the date.
To select the row of today's date, I would like to use something similar to getDay() to get the current days through the year (e.g. for 2020-02-11, the 42nd day of the year).
I've found the getDay() function in the docs but this is by month.
To avoid a function that calculates this (or a superfluous field in the source sheet, or scanning the rows for a date), is there a native function or simple way in google apps script to get the number of the current day of the year?
You could try using the JS date format with Utilities.formatDate() to get the day of the year like so:
function dayOfTheYear() {
var d = Utilities.formatDate(new Date(), 'UTC', 'D');
Logger.log(d);
}
In short, no; there's not a native function for getting the current day of the year.
The getDay() method you referenced is part of the ContactsApp, which means you would have to instantiate that, make any necessary calls to get a DateField object, and then call that method. That operation, although native to Apps Script, would likely take much longer than a simple JS function like any posted here.
Apps Script is basically just JavaScript with some additional objects that allow you to easily integrate with Google services. It has very few native "general" methods like what you're looking for, but most would be listed in the Utilities class.

Why is Time not setting when inserting new task using Task Service?

I am trying to insert new task with due date and time in Google Tasks using Tasks Service. The issue i am facing is that date is setting correctly while time is not setting. The code is given below:
I had tried all the solutions given on stackoverflow and other platform, none of them worked.
var task = Tasks.newTask().setTitle(data[i]);
task.setDue("2019-6-25T10:10:10.000Z");
Tasks.Tasks.insert(task, taskList.id);
I expect both the date and time to be set but only date is setting.
The official document says as follows.
Due date of the task (as a RFC 3339 timestamp). Optional. The due date only records date information; the time portion of the timestamp is discarded when setting the due date. It isn't possible to read or write the time that a task is due via the API.
Unfortunately, in the current stage, by above specification, even if 2019-6-25T10:10:10.000Z is used for due, it becomes 2019-06-25T00:00:00.000Z. So it seems that in order to modify the time, it is required to manually modify it.
Reference:
Resource representations of Tasks

Filter date range on a chart in Google Apps Script

I have a spreadsheet with monthly data going back nearly a decade and am creating a dashboard for this and other sheets like it.
I am attempting to filter the data table for a chart showing the last 12 months of data. I've attempted to use a number range filter on the dates using getTime() or valueOf(), but both refuse to to build a filter when I set a minimum value using date.valueOf() (I assume because of size). I've also tried the solution in the link below, using viewWindow, but that failed.
Date Range Google Chart Tools
I know this can be done with the visualization APIs, but unfortunately I'm stuck with Google Apps Script for this. I'm trying to use one data table for everything, but if this is impossible I can create a separate one with just 12 rows. I was really hoping to let users load custom ranges of time, but there's no ChartRangeFilter in GAS.
As you've found, the NumberRangeFilter doesn't work on dates, and Apps Script currently doesn't support any other range filters. One workaround I've employed is to convert your dates to numbers, and use a NumberRangeFilter to filter them. For example, "October 19, 2012" could be converted to "20121019".
In order to avoid weird jumps in the graph due to the whole between 20121031 and 20121101, you need to set the filter on the numerical date but use a DataViewDefinition to only show the actual date in the graph.
Here's a rough piece of sample code. Column 0 is the actual date, 1 is the numerical date, and 2 & 3 are values I want to plot.
var dateFilter = Charts.newNumberRangeFilter()
.setFilterColumnIndex(1)
.build();
var view = Charts.newDataViewDefinition()
.setColumns([0, 2, 3]);
var areaChart = Charts.newAreaChart()
.setDataViewDefinition(view)
.setStacked()
.setDimensions(800, 400)
.build();