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

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.

Related

Copy Adjacent Cells alongside Duplicate Cells

I have a very large Google Sheet spreadsheet that I need help with.
I have a long list of Network Switches that are very often repeated that I am trying to automatically copy the associated SKU into a separate adjacent cell.
My goal was, once the SKU is added the first time, whenever the same switch is added again, it will autopopulate with the same SKU.
For instance, C2461:C2463 are repeated, and once K2461 is populated, would like K2462 & K2463 to follow suit.
Any help would be greatly appreciated!
You can try this formula on Column L (col L will serve as a helper column):
=iferror(arrayformula(VLOOKUP(C2:C,C2:K,9,False)),"")
For now I can't seem to fit the formula without the use of a helper column (so I created a separate sheet for now that will serve as a database for the VLOOKUP formula) but this should get you started.
Sample using a helper spreadsheet (Database):
=iferror(arrayformula(VLOOKUP(Database!A2:A,Database!A2:B,2,False)),"")
Output:

Cut a portion of a cell value then edit and paste the results into a separate cell

I have a cell value that contains an address. I'd like to cut a portion of that data then edit and paste the results into a separate cell. I don't know the specific language to clearly outline my question so any help would be appreciated. I created annotated images to help.
Original .CSV data I imported into Google Sheets
The data from that import that I would like to extract within the cell.
Data output once I run the Macros script. Original cell data is copy, edited, and pasted into next column.
I have thousands of data entries like this so ideally this script would:
Cut all data starting at 'County:' from Premise Address column.
Paste that data into the County column while removing 'County:'
I ended up using the Split Text to Column function in sheets. Then Find and Replacing the remaining unnecessary text. It's not pretty but it worked.
Gonna dig into REGEXEXTRACT formulae tonight for some fun reading. Thanks!
There are multiple different ways to do this but the simplest would be to use the REGEXEXTRACT formulae; if the data structure is exactly the same across all samples, you could use the following one in the County column -
=IFERROR(REGEXEXTRACT(A2,"County: (.*)"))
Assuming that the first Premise Address is in A2 cell.

How to alter imported dataset without it disappearing?

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

Import dates in google SpreadSheets with importxml

I am trying to copy the data on this page (http://www.progsport.com/icehockey/) in my google spreadsheet.
I need all the data in the table and I want to separate them. I managed to copy the column of the matches with the instruction:
= IMPORTXML (B7; "// td / pre"), where B7 is the url.
Now I want to copy the rest but I do not know because they are special classes. I did several tests and I do not get it. How can I find that data, for example the number of (Photo1) of all the rows?
In other cells there are images. I want to copy the links of the images to treat them later. How do I copy those links in my document? (Photo2)
Thank you so much for your time.
How about this answer? I put http://www.progsport.com/icehockey/ to "A1" for this sample.
For Q1 :
=IMPORTXML(A1, "//tr[contains(#class, 'f')]")
Result :
Columns C - F are images. So data cannot be retrieved as the text data.
For Q2 :
=ARRAYFORMULA("http://www.progsport.com/" & IMPORTXML(A1, "//tr[#class='f2']//img[#class='im']/#src"))
Result :
If this was not useful for you, I'm sorry.

Convert a text string to a formula in a spreadsheet

I'm trying to develop an interactive spreadsheet that creates a narrative for a budget document. There will be a variety of options. Once the user selects an item, it will help them calculate the total. I want to setup option boxes that they fill in. For example, four cells will be allowed for input B1:B4. I am going to name each of the four cells (i.e. A, B, C, D). In the reference document I want to write various formulas. In some cases I might need "(A+B)*C" in another I might need "A * B * C" or "(A+B+C)/D" ... The spreadsheet would lookup the text formula, and I want to then convert it. So in the case of the lookup finding "(A+B)*C" I want it to convert it to =(indirect(A)+indirect(B))*indirect(C) which would then get the values from A (which is B1), B (which is B2) and so on.
Essentially, I would like to use or create something that is exactly the opposite of Excel's FORMULATEXT() function. I would prefer to do this in Google Sheets but I am willing to use Excel if I need to. I have a very basic understanding of both Google's scripting and VBA, and am willing to create if necessary, but not even sure how to tackle it.
Suggestions?
I found a way to do it in Google Apps Script:
function reCalc() {
var sheet = SpreadsheetApp.getActiveSheet();
var src = sheet.getRange("J26"); // The cell which holds the formula
var str = src.getValue();
str = str.replace('A', 'indirect("OPTA")').replace('B', 'indirect("OPTB")').replace('C', 'indirect("OPTC")').replace('D', 'indirect("OPTD")').replace('ENR', 'indirect("ENR")');
var cell = sheet.getRange("J30"); // The cell where I want the results to be
cell.setFormula(str); // Setting the formula.
}
Thank you to SpiderPig for giving me the idea!