Apps Script copy data in to formatted area? - google-apps-script

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

Related

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

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

How to apply a formula to a certain cell in Google Sheets when row is updated and a certain cell has a certain value?

I have a Google Sheet is being updated through two channels, an App through Appsheet, and an automated workflow from Zapier. Both add new rows for new bookings. The updates which come through the App work perfect, this is the main way of working. Now, I want to add information coming in through Zapier. Problem is that not all the information coming from Zapier is in the right format.
Please see the Sheet here.
If the data comes through Zapier, a new row will be created by Zapier and all columns except for Column C 'Driver ID'. I need this cell to be automatically updated. I can do this with an IndexMatch linking to another sheet with an overview of all the names and ID's. However, Zapier will always create a new row and therefore there will be no formula.
If data comes in through Appsheet all columns will be filled out except for Column E 'Driver Name'. There is no need for this since this information is already included in the Driver ID.
What I want to do is, apply a formula to Column C, if a value appears in Column E. If no value appears in Column E than there is no need for a formula in Column C. How can I trigger the placement of an (IndexMatch) formula for the cell when a certain cell has a text-value?

Google form inserts sheet row in unexpected location

I am having a strange issue regarding in which row of a linked sheet a google form places a new form row. Normally, each form submit adds a row to the sheet, starting with row 2 (row 1 is the header), as expected. If I delete all the rows in the sheet, form submits restart at row 2, as expected. But, now suppose I manually populate say the first 10 rows of the form (again, leaving row 1 as the header, so rows 2-11). Now when I do a form submit, I would expect row 12 to be added to and so on but instead, form starts at row 2 and wipes out my pre-populated row data. It seems that the form and the sheet have an internal "next row number" that is not based on the actual contents of the sheet. Can anyone shed any light one this?
I run into same problem. And found solution (after 2 hours of digging, where is the problem)
You need to use a function
sheet.insertRowsBefore(rowIndex, numberOfRowsToBeInserted)
first to create new rows (space) for your data and AFTER add your data to those created rows.
You can add new rows manually in google sheets of course - it works the same.
Explanation: There is an internal form property which holds a row index where a new form submissions will goes after a form submit.
When you only fill data, the index for new form submissions stays at previous position, so it rewrites your (any) data in cells.
Expect similar behavior when deleting a row data - if you delete only values (by delete key for. eg), then there will stay empty row when new form submission appears. Again you need to use function sheet.deleteRow(index) to delete data and change index of new row for new submissions.
Sounds like you are using the getActiveCell() or getActiveRange() method to find the last row. Whereas, you should be using getDataRange() method to get the last row and columns. getActiveCell() or getActiveRange() methods point to the location of the cursor on the Spreadsheet. Hence, if at the time it was at Row 1 - Col2, that's where it'll insert your info. Refer to this documentation for more information.
This happens by design so that form submissions never overwrite user inserted data. This is well demonstrated in this response[1].
I believe you could resolve problems if you create a script to insert your older data using the createResponse() method[2].
[1] https://support.google.com/docs/thread/10244473/google-forms-inserting-at-top-of-sheet?hl=en
[2] https://developers.google.com/apps-script/reference/forms/form#createResponse()

Google Sheets Pivot Table Not Updating

I have a google app script which submits info to an organized sheet and would like to create a pivot table with the all information in the sheet. I can do this, but whenever I submit a new row of data to the sheet, it is not automatically included in the pivot table. I have to manually change the range of the pivot table every time I submit a new row of data. Is there any way I can make the sheet/pivot table automatically include the new row of data?
I worked around this issue by only specifying the column range.
For example, if you have row data in columns A to F, set the range of the pivot table to SHEET!A:F
If you add rows now, the new data in those columns will be added to the pivot table.
There is a way. I did exactly that 3 years ago on Sheets.
If you are submitting the new rows using Google Forms, then there is no way.
If you try to programmatically update the range, there is no way either because named ranges need to be deleted then re-added, causing #REF on the pivot.
Now for the good part: If you are adding the new row with a script, do not append it to the end. Instead, keep a fake row at the end (with zero values so pivot is not affected) and insert the new row just before it. Your range (named or not) will update.
Even for the Google Forms case, you might get it to work by pre-adding all blank rows to the response sheet and make the named range include the blank ones too.
I encountered the same problem with the Google Sheet linked to a form. How I solved it:
Edit the Pivot table range to include the empty row after the last row in your sheet.
If your pivot table data range is 'Sheet Name'!A1:S100, change it to 'Sheet Name'!A1:S101.
Add a filter to your pivot table for a column (present in the data range) that will never be blank and set the condition for this column - 'Is not empty'
If you have a column 'Timestamp' (and you're sure it will never be empty), then add this column to the filter, select 'filter by condition' and set the condition to 'Is not empty'.
These steps will filter out the last (empty) row. When you will add a new row to your data, the pivot table will update automatically and you won't have to update the data range again.
In my case what was interfering were the fields in the Pivot's Filters section (highlighted in red below).
Make sure you test without any filters... see if it works. Then add your filters one by one and play with the pivot. Probably the filter(s) is\are influencing the pivot to not update.

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.