How to alter imported dataset without it disappearing? - google-apps-script

I have imported a data set from Sheet A to Sheet B in a google sheets workbook using a Query function (=Query(Original_Data!A1:G, "SELECT B, C, -1*D, E, F, G",1).
I am now looking to alter some of the data in the copied data set (Sheet B), but cannot do so without everything disappearing.
I realize this is likely due to how Query works - so I am looking for an alternative solution to copy the data set from sheet A to B (regularly).
I am considering creating a macro that can repeat a simple copy and paste with the click of the button.
I am not sure if this is the best solution.

If you only need copy a RANGE, use the formula IMPORTRANGE
if you need alter the query, you need to do via query lenguaje
https://developers.google.com/chart/interactive/docs/querylanguage
query lenguaje is like SQL, if your data is normalize probably you can go in this way

Yes it is the best solution. You are, so to speak, wanting to have your cake and to eat it - so bake another.

If you use Apps Script, you can use the methods getValues() and setValues() to copy ONLY the values, so that you can alter the data in both sheets independently. You can run your Apps Script code as a macro. Here some useful links:
https://developers.google.com/sheets/api/quickstart/apps-script
https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()
https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues

Related

google sheet apps script truncate column value

I have the following table:
I am simply trying to write a Google apps script to insert into column E everything in column B prior to '.Upload'. So in the table column E = 20ba4a5c.
I think I should be able to use the split() function to do that but I'm having some difficulty.
ss.getRange('E'+lastRow).setFormula('SPLIT(B'+lastRow+'.Upload')[0]');
You should use REGEXEXTRACT to build a simple regex in order to
achieve your goal.
It is also a better practice to use template literals when dealing with multiple concatenations.
Solution:
Replace:
setFormula('SPLIT(B'+lastRow+'.Upload')[0]');
with:
setFormula( `REGEXEXTRACT(B${lastRow},"^(.*?).Upload")`)
Output:

Google Spreadsheets ArrayFormula: How to split and transpose a cell-range?

Hello everybody and thanks a lot for your help.
Here's my problem:
What I have:
I have a table with raw data in 53 rows and numerous columns which I would like to reduce and restructure into three columns: City, Date and Value.
https://docs.google.com/spreadsheets/d/1bsdC8lrtSGk957ae8Z0VRGnDqTZfFLPpLkfoid0UbIQ/edit?usp=sharing
What I've done so far:
For a single row, I used the following formula to make everything work as I wanted it to:
ArrayFormula({SPLIT(TRANSPOSE(Base_Data!A2)&"|"&TRANSPOSE(Base_Data!AJ1:1&"|"&Base_Data!AJ2:2),"|")})
What I want:
I'd like to extend the formula to work for the entire area, all 53 rows. Does anyone have a tip for this? The solution doesn't have to be a formula, it would work as a script, too
I've set up a new sheet called "New_Data [Erik]" and placed the following formula into A2:
=ArrayFormula(SPLIT(FLATTEN(Base_Data!A2:A&"\"&Base_Data!AJ1:1&"\"&Base_Data!AJ2:54),"\",0,1))
If this is a one-time conversion, I'd recommend copying the results in place. To do that, select A:C, hit Ctrl-C to Copy and then Ctrl-Alt-V to Paste Special. A small clipboard icon will appear. Click it and choose "Paste Values Only."
If you'll need this functionality ongoing, just understand that FLATTEN is a not-yet-official function of Google Sheets, which means that while Google sheets may very well make it official, they may also decide to do away with it at any time. (This is why I suggest copying and pasting the results in place, if it's just a one-time conversion.)
Not sure what you're trying to get to there. If you are trying to leave out all columns but 3, just do ={Base_Data!A2:A, Base_Data!E2:E} and add as many columns as you require comma-separated within the curly brackets

How do I copy value from a goole sheet to a google doc using scripts for long format?

I have:
A Sheet with Columns for A, B, and C.
A Doc with a templatized text that includes A, B, and C values.
A output Doc where I want to have the templatized text multiple times, adjusting for A, B and C.
Is there a way to get a script that does this?
Reading your steps I can see that you are very close to finishing your project. You only need to use the following methods.
First you would need to open and read your spreadsheet. You can gather all the data with a combination of Spreadsheet.getDataRange() and Range.getValues(). You will end up with an array with the A, B and C columns.
Furthermore you would need to open the Doc template. This step is very similar to the previous one, you can achieve it by using Document.getBody() and Body.getText().
Now you have the data and the template, you only need to join them together. To manage it, run the data rows in a loop substituting the placeholders in the template with the data rows. This way you will end up with an output array as big as your spreadsheet data. At this point you can write it down on the output Doc by using Document.getCursor() and Position.insertText() with each output line.
Please, feel free to ask me any doubts to better help you.

Alternative to copyTo or setValues?

I'm currently copying one tab of a sheet to a completely different sheet as a backup. I use copyTo() for this because I want to copy the entire tab. When I update this backup, I use getValues() -> setValues() to overwrite the previous data with a particular range so as not to overwrite the formulas within the headers. But of course what happens is all of my data validations within the array that were set during copyTo() get overwritten with static data.
What function should I use that will not only copy the values but also the data validations (maybe even the formulas)? I would use copyTo() again, but the sheet name already exists. Would renaming the sheet first and then copying be the way to go?
If you really need to apply data validation rather than just copyTo again there is some help here: https://developers.google.com/apps-script/reference/spreadsheet/data-validation
Otherwise renaming the old copy or deleting it if you don't need it is a simple solution.

use JOIN to pull multiple IMPORTRANGES into SORT(ARRAYFORMULA({importrange1; importrange2; etc}

How can I use JOIN (and maybe VLOOKUP? FILTER?) to make a list of IMPORTRANGES, resulting in something like {IMPORTRANGE(C3,$E$1); IMPORTRANGE(C4,$E$1); IMPORTRANGE(C5,$E$1); IMPORTRANGE...}?
Currently, in a google sheet, I have a formula that looks like this: =SORT(ARRAYFORMULA({IMPORTRANGE(C3,$E$1);IMPORTRANGE(C4,$E$1);IMPORTRANGE(C5,$E$1);IMPORTRANGE..." where spreadsheet urls are in Col C and a range (same for every imported sheet) is in E1.
Typing it all in was fine when I only had about a dozen spreadsheets I was importing and they all already existed. But now I want to import many more spreadsheets (I heard that the limit of 50 importranges no longer applies) and they don't all exist yet. If I keep things as they are, every time I add another spreadsheet url to Column C, I'll also have to go in and edit my =SORT formula.
Then I found this thread, Fill ArrayFormula with dynamic ImportRange, which has a suggested answer listed as: ="=sort(ARRAYFORMULA({"&JOIN(";",ArrayFormula("IMPORTRANGE("""&VLOOKUP(FILTER(G2:G20,G2:G20<>""),Sheet3!$A$2:$B,2,0)&""","""&G1&"!A2:B"")"))&"}),1,True,2,True)"
The JOIN in there looks intriguing (I just recently learned about JOIN) but I don't understand all the syntax (like """) and I also can't access to original spreadsheet to see what the references point to.
So, I'm looking for help in how to input the C3, C4, C5, etc into the JOIN -- not sure how the VLOOKUP helps me -- and also if anyone knows why there are so many ="&=&"""s throughout the suggested formula...
One of the solutions below may do what you need. The first is what you requested. The second is the one which will probably work to combine multiple IMPORTRANGE formulas.
SOLUTION 1
="="&ArrayFormula(REGEXREPLACE(QUERY(UNIQUE(TRANSPOSE(SPLIT(CONCATENATE(IF(E3:E="","","IMPORTRANGE("&E3:E&", $E$1)"&":")),":"))),,9^99),"\)(.*?)I","\), I"))
SOLUTION 2
="=QUERY({"&ArrayFormula(REGEXREPLACE(QUERY(UNIQUE(TRANSPOSE(SPLIT(CONCATENATE(IF(E3:E="","","IMPORTRANGE("&E3:E&", $E$1)"&":")),":"))),,9^99),"\)(.*?)I","\); I")&"},""Select Col1 where Col1<>''"",0)")
You would then just copy the cell, select the destination cell and "paste as values" into the formula bar.
You can see the sheet in action HERE (just make a copy of it to use).