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:
Related
I have these 2 columns in my sheet: A and B.
A contains dates in format day.month.year. B contains hours in format hh:mm. I usually concatenate the values of these 2 columns by using this formula:
=concatenate((text(A3;"mm.dd.yyyy")&" "&text(B3;"hh:mm")))
I use the same formula to concatenate the values of 2 other cells: C (filled with dates!) and D (filled with hours!). The exact formula in this case is:
=concatenate((text(C3;"mm.dd.yyyy")&" "&text(D3;"hh:mm")))
The whole purpose of this little exercise is to get the dates and hours in the format that is required to create events in my Google Calendar. I am currently using it in my calendar event creation script with the method setFormulaR1C1(formula), but I am not sure if that´s a really good idea. So, please tell me:
How would an apps script equivalent of my formula look like?
And is there a way to write that apps script equivalent of my formula as a callback function?
Thank you so much in advance!
How would an apps script equivalent of my formula look like?
Given that the cells already display the date and time in the format you need, you can simply use this:
const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
const datetimeString =
sheet.getRange('A3').getDisplayValue()
+ ' '
+ sheet.getRange('B3').getDisplayValue();
If the cells do not display the date and time in the required format, you will need to use Utilities.formatDate() and Spreadsheet.getSpreadsheetTimeZone(). There are some complications when interpreting spreadsheet time values in Apps Script. Refer to the recipe at Google Script when taking time from sheets adds +1 minute to avoid surprises.
To get the same with a spreadsheet formula more easily, use this:
=trim(A3) & " " & trim(B3)
I'm using #Wiktor Stribiżew 's custom function ExtractAllRegex(). The script extracts all occurrences of a Regex pattern. In the example, I extract all words in column A starting with "v_"
Here is a Google Sheet showing what I'm trying to do.
The original strings are stored in column A. The custom function/the matches are in column B.
Wictors function works great for single cells. It also works great when I manually drag the formula down the column.
Here's Wictor's original code:
function ExtractAllRegex(input, pattern,groupId,separator) {return Array.from(input.matchAll(new RegExp(pattern,'g')), x=>x[groupId]).join(separator);}
Description:
input - current cell value
pattern - regex pattern
groupId - Capturing group ID you want to extract
separator - text used to join the matched results.
The question is, how do I turn column B into a working array formula? Or, perhaps better, how do I modify Wictor's script so it accepts a range instead and auto-fills down column B?
I updated your script to:
function ExtractAllRegex(input, pattern,groupId,separator) {
return input.map ? input.map( inp => ExtractAllRegex(inp, pattern, groupId, separator)) :
Array.from(input.matchAll(new RegExp(pattern,'g')), x=>x[groupId]).join(separator);
}
and changed the formula in B2 to
=ExtractAllRegex(A2:A13,"(v_.+?\b)",0," ")
See if that works for you?
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
Excel Example - conditional formatting
No, but a crude alternative is mentioned here: Gsheets-databars
Use the formula function REPT to create a bar graph consisting of the pipe character "|", repeated a number of times based on the value in the adjoining cell.
=REPT("|", B1)
I searched and could not found any way to do it currently it not possible.
I just discovered Google App Scripts, and I'm stumped on something already...
I am trying to write a script for a Google Spreadsheet which finds certain historical stock prices. I found that the FinanceApp service within Google App Scripts has been deprecated, and seemingly replaced by the GOOGLEFINANCE() function within Google Spreadsheets. However, it returns an array, when I need only a single cell, and the array is mucking up the works.
So I'd like to write a short script that calls the GOOGLEFINANCE() spreadsheet function, and finds just the 1 piece of info I need from the array which is returned by GOOGLEFINANCE(). However, I cannot find a way to access Spreadsheet Functions (SUM, VLOOKUP, GOOGLEFINANCE, etc) within a script.
Is there a way to access these functions in a script? Or perhaps, is there a new service which replaces the deprecated FinanceApp service?
Many thanks for any assistance!
You can try this:
var trick = SpreadsheetApp.getActiveSheet().getRange('D2').setValue('=GOOGLEFINANCE("GOOG")').getValue();
Native Spreadsheet functions are not supported in Google Apps Script.
You could eventually use a somewhat cumbersome workaround by reading the value of a cell in which you write a formula (using script in both write and read) but this will be less than practical and / or fast.
You might try the INDEX function combined with GOOGLEFINANCE-
For reference,
=GOOGLEFINANCE("MSFT", "PRICE", "01/01/21")
Returns the array:
Date Close
1/4/2021 217.69
One can add the INDEX function to pick out specific elements from the array using the row,column coordinates of the array.
=INDEX(GOOGLEFINANCE("MSFT", "PRICE", "01/01/21"),2,2)
This returns just the data in row 2, column 2 - 217.69
There is one possible way, with the .setFormula(). This function behave like .setValue() and can be used the following way:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mySheet = ss.getSheets()[0]
//Code Below selects the first cell in range column A and B
var thisCell = mySheet.getRange('A:B').getCell(1,1);
thisCell.setFormula('=SUM(A2:A4)');
All formulas you write in this function are treated as strings must have ' or " within the .setFormula() input.