Laravel to Google Sheets new lines not starting at beginning - laravel

I'm using this package to store data from a Laravel app into Google Sheets: https://github.com/kawax/laravel-google-sheets/
Everything works fine except for one strange issue. Here's how I add new lines to the sheet:
$sheet = Sheets::spreadsheet("sheet_name")->sheet("tab_name");
$sheet->append($this->data);
$this->data contains an associative array with the column names and values.
Most of the times the new lines are added as desired. But sometimes they start in column B with everything shifted over. And now today they are randomly starting in column V.
I can see that the last line in that sheet has values up to column U, so there must be a connection, even though this new line is added five lines lower.
I've not been able to specify a range for the append method. Is there a way to force Google Sheets to store the data starting from A?

Related

Google Sheets Row Number of nth Instance from a column in another spreadsheet

I have used the following google sheets formulas to obtain the row number for the nth instance of cell k3 within column A.
=ArrayFormula(SMALL(IF($A$4:$A$20000=K3,ROW($A$4:$A$20000)-MIN(ROW($A$4:$A$20000))+4),n))
I also used the following formula but it only works for the 1st instance.
MATCH(K3,A4:A20000,0)+ROW(A4:A20000)-1
I need to get the row number of the nth instance but for a range in another spreadsheet. I have tried replacing every instance of $A$4:$A$20000 in the 1st formula above with the string below but to no avail. I also tried it without the IMPORTRANGE().
IMPORTRANGE("myurl","mysheetname!$A$4:$A$20000")
Please help me to get that row number. Cheers.
Try:
=filter(row(A4:A2000),A4:A2000=K3,countifs(A4:A2000,A4:A2000,row(A4:A2000),"<="&row(A4:A2000))=K4)
Where cell K3 is what you're looking for and K4 is the instance number (which you can hard code if you want).
Update for your sample sheet:
To bring in the data from another sheet, load it into your final sheet rather than trying to put the importrange() in the formula:
Then the formula in cell A9 can reference the imported range (identifying the row number for the second instance of "BAG1"):
=filter(row(A2:A6),A2:A6="BAG1",countifs(A2:A6,A2:A6,row(A2:A6),"<="&row(A2:A6))=2)
I got this to work...
=VLOOKUP("BAG1"&3,IMPORTRANGE(url,Sheet1!A2:C6),3).
It finds the instance of BAG1 that has 3 in the neighbouring column and then retrieves the value in column 3.

Insert a new Google Sheets row into Alphabetically Sorted spreadsheet

I am working with two spreadsheets; the first spreadsheet takes a name and then automatically adds it to the next spreadsheet which is sorted alphabetically by name. The problem is, I need a new row to be created, otherwise the data from the row above it gets added along with the name. Here is the query I am using: '=query(Referrals!A2:O, "select * where C is not null order by D")'. I don't think this can be done with a query, so I have been exploring Google App Scripts. I am not sure how to insert into the pre-sorted list, though. Any help is greatly appreciated!
Have you tried offsetting the header so you can run it for the range of the sheet?
=query(Referrals!A:O, "select * where C is not null order by D Offset 1")
That seems to fix some of the issues I've come across.
EDIT:
What about using a filter formula?
=sort(FILTER(offset(Referrals!$A:$O,1,0),offset(Referrals!$C:$C,1,0)<>""),4,true)
If using Apps Script, then you can directly insert a row (via Sheet.insertRows(rowIndex, numRows)) into the sheet at the desired index. But I believe you can achieve what you want by mapping the data in "next spreadsheet" to the names imported via "query" using VLOOKUP. That way when new data is added to "first spreadsheet" it will be sorted accordingly with your formula, but now the data associated will move rows to continue matching their respective row.

Generate new format from a non-system generated report using Power Query

I have an excel file which is non-system generated report format.
I wish to calculate and generate another new output.
Given the Report format as below:-
1) Inside the query when load this excel file, how can I create a new column to copy and paste on the first found value (1#51) at column at the next record, if the next record is empty. Once, if detected a new value (1#261) then copy and paste to the subsequent null value of few next records till this end?
2) The final aim is to generate a new output to auto match/calculate the money to be assign to different reference. As shown below:-
The reference A ~ E is sharing the 3 bank Ref (28269,28542 & RMP) , was thinking to read the same data source a few times, first time to read the column A ~ O(QueryRef) and 2nd time to read the same source to read from A, Q ~ V(QueryBank).
After this I do not have idea how I can allocate the $$ from Query Bank to QueryRef based on the Sum of Total AR.
Eg,
Total Amt of BankRef 28269, $57,044.67 is sufficient to cover Ref#A $10,947.12
BankRef 28269 still sufficient to cover Ref#B $27,647.60
BankRef 28269 left only $18,449.95 , hence the balance of 28269 be allocate to Ref#C.
Remaining balance of Ref#C will need to use BankRef28542 to cover,i.e. $1,812.29
Ref#D will then be allocated of the remaining balance of BankRef28542, i.e. $4,595.32
Ref#D still left $13,350.03 unallocated, hence this will use BankRef#RMP
Ref#E only need $597.66, and BankRef#RMP is sufficient to cover this.
I am not sure if my above case study can be solved using power query or not, due to me still being a newbie # Power Query? Or this is too complicate to handle hence we need to write a program to auto matching this kinds of scenario?
Attached is the sample source file and output :
https://www.dropbox.com/sh/dyecwcdz2qg549y/AACzezsXBwAf8eHUNxxLD1eWa?dl=0
Any advice/opinion/guidance is very much appreciated.
Answering question one:
You have a feature in Powerquery called FILL, DOWN or UP.
For a selected column you can copy the first non empty value to all rows under until a new non empty row is found and so on.

Script for copying and pasting value only (GOOGLE SPREADSHEETS)

I have the following idea I can't implement:
There is a row with given table plate numbers (column B) and there is a second row with names which are copies by "vlookup" formula from another spreadsheet and in case there are no numbers found, the text should be filled in manually. I have added the formula which leaves cells blank:
=if(isna(index('spreadsheet1'!A4:A1002;match(B16;'spreadsheet1'!B4:B1002;0)));"";index('spreadsheet1'!A4:A1002;match(B16;'spreadsheet1'!B4:B1002;0)))
But sometimes the person assigned to a car plate changes and thus, after taking this value, I have to make as easy as I can to paste values only (the problem is the person who will add data may make mistakes) - I have created a button and want to assign a script which will check if the value in the column B has a name (not empty =""), then it should copy it and paste value only at the same place but all other cells which will be empty should stay with formulas inside them for further addition of a person to a new plate number or to be added manually.
Every new column will be filled in one by one and would like this script to work for constantly. Do you have any ideas or hints how I can implement it?

Is it possible to get the last row filled?

In gspread, is it possible to get the last row or cell number that is filled?
The API reference page doesn't seem to help.
Gspread has "row_count", but I am not sure it returns all rows in a spreadsheet, or just the filled ones. If that doesn't help, there is a slightly less direct, but completely functional way to do it:
1.) let's assume your data is in column A, and it has been filled in consecutively row by row (i.e. no skipped rows)
2.) in another free cell in your spreadsheet--let's assume cell B1--use the native Google Sheets function COUNTA, which will count the number of values in a dataset, and specify column A as the target, i.e. "=COUNTA(A:A)"
3.) now just request the value of cell B1 with gspread's acell, i.e. "last_row_updated = myWorksheet.acell("B1").value"
You can use the following code, taken from this answer to this similar question:
def last_filled_row(worksheet):
str_list = list(filter(None, worksheet.col_values(1)))
return len(str_list)

Resources