Automatic S# column when using filter/query in google sheets - google-apps-script

Here what i am trying to do
I want to automatically generate the serial number in the column while filtering. i tried to use below formula
=SUBTOTAL(3,$B$1:B)-1
Which is perfectly working for individually entered data.
but I want to apply the same function on a different sheet which is getting data by query formula and filtering it by different dropdowns.

oK I FIGURED IT OUT
i put 1 in the 1st raw of my data and put below formula to count as per shown results.
=IF(ISBLANK(B12),"",A11+1)
Thanks

Related

Apps Script copy data in to formatted area?

So I'm sending data from an app to a google sheet and then trying to format the data. If I pre-format the cells in anticipation of the data, the rows of data end up being appended below the last formatted row. I'm also trying to base other cells off the anticipated data that is being imported, but the rows get appended below the last row with data, even if it's not in the way. For example, if I'm always importing 1 row and 3 columns at a time, I would like to prefill column 4 onwards withe formulas and have my data slot into the same row. Is this possible with google apps script? Or is the only way to have the row of data appended after the last row in the sheet? Ideally, I'd like it to be the first available row with the first 3 columns empty.
Using appendRow when you want to that the new row gets the same formatting of the above rows is "tricky". The safe way to go is to use the script to set the format of the appended row. One way to do this is by using copyTo with SpreadsheetApp.CopyPasteType.PASTE_FORMAT

google sheets query first row mess

I am designing a workflow at work to streamline some tasks. I get some CSV from a database which I want automatically added to a Google Sheet. I stored it on a Google Drive, got the direct download link and used it with IMPORTDATA on sheet1. So far so good.
The final aim is to create a good way to visualize which rows meet specific conditions, so I thought using a PivotTable might be handy. However, to filter out some of the uninteresting rows, PivotTables are not the way to go, since the filters are quite poor (no possibility of basic filter criteria like =, !=, <, >, etc). So I thought about populating a second sheet with a Query function (with the filtering statements) so that the PivotTable generates a report directly from a filtered source.
The issue (apart from the lag) is that sheet 2 (the one with the query) shows a strange output: the first row, that should contain only the column labels, mixes up the column labels with the value of the cell below them. It is quite baffling. Moreso because it does not happen all along row 1, but it only appears to do so on the first seven columns. So insted of A1 showing "event_id", it actually shows "event_id 106727", which is the value of the fisrt event that should go on A2.
Any thought why this happens?I already checked and there does not seem to be anything wrong with the CSV.
If yyou also have a suggestion of why this whole setup would be a bad idea, please also tell me :P
Thank you!
The query command combines multiple header rows into one. And sometimes it mistakes your data for headers. To avoid this, use the optional third parameter to specify the number of header rows:
=query(range, query_string, 1)
(if there is one header row).
The approach by #user6655984 did not work for me, because I was already using the third parameter to customise my header usage.
What occurred was that I was performing 12 queries to different sheets with similarly data. One column was a mixture of strings, such as "12 FPS", "14fps", and numbers. When I began working my way through these columns on all the sheets, I was left with mostly numbers, with a few strings spread throughout.
This caused my query to create a massive first row which concatenated the results of about 6 rows together, cell by cell, before showing the rest of the results, where in this column I edited, all the strings had been removed.
By happenstance I saw this one person here who talked about fixing a query issue by making sure that they type-converted the information in a column to be consistent.
This led me to find the following Google support forum post, about queries and data types, from 2019: Query function not working with specific data set (but working with others)
Giving this a go, I tried turning all the data in all the columns that were equivalent to each other in each sheet I was querying into numbers fixed the issue. Try this if setting headers is not working; make sure you don't have silent type errors.

Imported range time values dont match source range time values Google sheets script

I have simple google spreadsheet with two sheets: sheet1 and sheet2. In sheet2 there are time values in column A which I am trying to copy to column A in sheet2. I wrote a script(please see link) for it but values are not the same. What class/method do I need to use in order to get same values in both sheets.
I noticed if I format column A values in sheet2 to text than I get matching values but problem is I am not allowed to that and that column must remain formatted as time.
Also, my second question(please ignore if its off topic). If I have work schedule in google sheet for 3 employees: John,Peter and Mike and they all have editing permissions. If Mike decides to use filter to filter out just his work schedule, is it possible for other two employees to have unedited sheet(as I believe Mike's actions will affect other two persons). What I mean is, is it possible to restrict one persons actions just to view he is looking at while other have unaffected version(together work schedule in this case)?
link for spreadsheet:
https://docs.google.com/spreadsheets/d/1tOtwzM0CxDHBXzC8ECeXlfaTGC4i7rH2XMYYszqRINM/edit?usp=sharing
Much appreciated.
not entirely sure what the problem is, your script is working as intended and you have the same values in sheet1 as in sheet2. The only difference is that in Sheet1 you have a date format and on sheet2 you only indicate the time. I have changed the number format in the sheet1 and as you can see it's the same.
The problem you are having is that by indicating only the time, you let the sheet assume any date. For time it's not that big a deal, but you must be sure that time formats are the same. use getNumberFormat() and setNumberFormat() (read here) to set the correct formats when copying or have the formatting set in advance
As for filtering — you can simply give them view-only access and let them use filtere views (right under "Filter"). A filter will hide information for everyone on the sheet, while a filter view will only be visible to that person. You can also pre-set filter views for them as those with edit access can save filter views.

How do I efficiently output to a non-contiguous range in Google Apps Script (GAS)

I'm new to Google Script, so appreciate your help!
Here's what my data looks like (3 non-contiguous records, 4 non-contiguous fields):
https://docs.google.com/spreadsheets/d/18FFB2HlcfcciHj7NPmihZbuf47op2UMdRTKfpyTqowU/edit#gid=0
I have an array of the items and each item is an object that contains 4 keys. I want to output to Google Sheets in as few SetValue requests as possible. If I can't do it in 1 call, then it makes most logical sense to output each item at a time.
My idea is that I can create ranges for fields 1 and 4 that span the entire column. Then I can create ranges that span the entire Item row. Then the INTERSECTION between the 2 is the range I want to output to, once I have assembled an array of 2 values.
Or perhaps since assuming I know the rows/columns of each cell below, I can return the range and the use a UNION of the ranges to create the mapping instead.
But is there a function to do Intersection or Union in GAS? Or am I better off just outputting each cell 1 by 1?
Thanks for your help!
There's no such function. It's either one by one or contiguous cells.
But there's quite a few tricks/alternatives that might work for you. As Sandy pointed in a comment, you could get a contiguous range that had all required cells and set the non-required ones to blank or their original values. This has the down side of not working for formulas.
You could also pre-configured the required cells with simple formulas "pointing" to a continuous range. That would simply bulk update at once.
If bulk setting is really required, you could also grab all values and formulas of the wide range, and convert all plain values to their equivalent formulas, e.g. abc becomes ="abc" and 1/1/2015 =DATE(2015,1,1) (yes, it's cumbersome) and then use setFormulas to set everything back, both original formulas and values converted to formulas. Making no actual content change in the cells you don't want to and changing the required ones, all in one bulk operation.
Anyway, these are just workarounds. As stated in the first sentence, it's not possible, period. You probably want to star this report in Apps Script issue tracker to kind of vote for this feature and receive updates.

Google Spreadsheet Chart to show Range of Dates

I am constructing a chart to show stock values from a Google Spreadsheet macro, using Google App Script. I read the spreadsheet values into Charts.newDataTable(), then construct a filter with Charts.newNumberRangeFilter(). Then I bind the filter and LineChart together in a dashboard.
Now, what I really want is something like google.visualization.ChartRangeFilter in the Google API, a selectable range of dates to show in a graph. However for some reason, this isn't available from spreadsheet macros. So I am trying to make an alternative using NumberRangeFilter.
The problem I am having is that NumberRangeFilter allots a space on the X-axis of the chart for every date within the min and max of the range, regardless of whether or not I supply data for it. However, stock data is only valid for Mon-Fri of each week. That leaves two days of blank horizontal space between each week, and is not the desired effect I want. Here is a screenshot: screenshot
I also tried using CategoryPicker, as that allows me to control the values used, and also allows the use of Strings for dates, but that only allows me to choose one date at a time to add to the graph, and is not a good solution. There doesn't seem to be a way to use this with a slider bar.
Does anyone have any idea how to implement this in Google Spreadsheet based AppScript?
If your data is sorted, you could set the number to be the row number, so the numbers are consecutive. However, the filter becomes less clear to users.
You can filter using a number column, and still use the date column or a string column on the chart. In the picture below, I converted my years and months to consecutive numbers, filtered it to the last 12 months, made the filter invisible, and used a string column for the x-axis for custom labels.
There is an open issue to add the ChartRangeFilter.