Replace text after importData function - google-sheets

I am using importData function in Google Spreadsheet to import an external csv file. It works, but I want to replace some text in the table.
If am not wrong for replacing text there is a function:
SUBSTITUTE("search for it","search for","Google")
The problem: when I am trying to use SUBSTITUTE I get error:
Array result was not expanded because it would overwrite data in A3
Is there any way to import csv and replace text in the document?

You can use arrayformula and substitute along with importdata to replace or remove data. For example, I used this to replace '=" and double quotes with blanks:
=arrayformula(substitute(SUBSTITUTE( IMPORTDATA("https://ct.thecmp.org/app/v1/index.php?do=match&task=downloadMatchResultsDetail&MatchId=12504&EventId=4&AwardId=1") ,char(61) , "" ),char(34),""))

Related

Regex to find URL without the file name and extension

I have a Google sheet with hundreds of images URLs from Dropbox and I want to write a regex that will find all the prefixes part of the URLs without the file name + extension. I'll then replace the search result with another url.
What I have:
https://www.dropbox.com/s/dtauvpuy3a5qyu4/A1001.jpg
https://www.dropbox.com/s/dtauvpuy3a5qyu4/A1001.jpg
https://www.dropbox.com/s/dtauvpuy3a5qyu4/A1001.jpg
https://www.dropbox.com/s/d0xedx0j72v5uub/A1002-1.jpg
https://www.dropbox.com/s/d0xedx0j72v5uub/A1002-1.jpg
What I expect:
https://anotherurl.com/A1001.jpg
https://anotherurl.com/A1001.jpg
https://anotherurl.com/A1001.jpg
https://anotherurl.com/A1002-1.jpg
https://anotherurl.com/A1002-1.jpg
Please, how can I do that?
Suppose your original URLs are in the range A2:A of some sheet. Clear some other column entirely (e.g., B:B), and place the following formula in B2:
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(A2:A,"(.+//).+(/[^/]+)$","$1"&"another.url"&"$2")))
Where you see "another.url", you can either manually enter some other URL text between the quotation marks, or you can enter the new URL text is some cell (say, B1) and use the cell reference instead:
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(A2:A,"(.+//).+(/[^/]+)$","$1"&B1&"$2")))

Returning value of an item price in google sheets

I'm trying add easy updating prices into a google sheet.
I need the market price from
//*[#id="app"]/div/section[2]/section/div[1]/section[3]/div/section[1]/ul/li[1]/span[2]
https://www.tcgplayer.com/product/242811/pokemon-celebrations-celebrations-elite-trainer-box?Language=English
I need it to display just the one number from the XPath to a cell, and I can't seem to figure out where I am going wrong. I've been using the IMPORTXML function and it won't return a value.
=IMPORTXML(A2,"//*[#id='app']/div/section[2]/section/div[1]/section[3]/div/section[1]/ul/li[1]/span[2]")
where A2 is the URL.
In your situation, it seems that the value of the market price cannot be directly retrieved from the URL of https://www.tcgplayer.com/product/242811/pokemon-celebrations-celebrations-elite-trainer-box?Language=English. But, fortunately, it seems that that value can be directly retrieved from the endpoint of API. So, how about the following sample formula?
Sample formula:
=REGEXEXTRACT(JOIN(",",IMPORTDATA(A1)),"marketPrice:(.+?),")*1
or
=REGEXEXTRACT(QUERY(TRANSPOSE(IMPORTDATA(A1)),"WHERE Col1 matches 'marketPrice.+'"),"marketPrice:(.+)")*1
The cell "A1" has the URL of https://mpapi.tcgplayer.com/v2/product/242811/details.
In the case of https://www.tcgplayer.com/product/242811/pokemon-celebrations-celebrations-elite-trainer-box?Language=English, please use 242811 from the URL to the endpoint of API like https://mpapi.tcgplayer.com/v2/product/242811/details.
Result:
Note:
The value from the URL is JSON data. In this case, the following custom function can be also used. In this case, please copy and paste the following script to the script editor of Spreadsheet and save the script. And please put a custom function of =SAMPLE("url") to a cell.
const SAMPLE = url => JSON.parse(UrlFetchApp.fetch(url).getContentText()).marketPrice;
References:
IMPORTDATA
REGEXEXTRACT
Custom Functions in Google Sheets
it's not possible to scrape JS content into google sheets:

How to paste into google sheets with line break or newline a delimiter?

I copy some text from Notepad++ and paste it into google sheets. It pastes it tab-delimited. I want it to be pasted as new-line delimited. Tabs shouldn't be taken into account for delimiting.
I tried by deleting extra columns, keeping just one. but it creates extra columns. In excel there is an option to import text with a required delimiter. In Google sheets, how to achieve this? If there is no such thing, can it be achieved via app-script?
Just in case. Here is the little snippet that splits contents of selected cell by lines and puts these lines into bottom cells:
function split_cell() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveRange();
var value = cell.getValue();
var cells = value.split('\n').map(x => [x]);
sheet.getRange(cell.getRow(),cell.getColumn(),cells.length).setValues(cells)
}
If you want to import the text from the txt file, you can simply choose the delimiter you want.
For instance, when using the Import option in Sheets:
You can select a custom separator:
However, if you are simply copying the text from the txt file, you may want to check the settings for the text editor you are using when it comes to the delimiter chosen.
Reference
Import data sets & spreadsheets.

Stop a '+' and other symbols from generating a formula with Google Sheets API

These cell texts = thinking, + thinking, =+ thinking show an error.
I found these workarounds:
enter an apostrophe ' as the first character, right before the +.
enter the contents as a string formula like ="+5 blah".
But, I need to do this using Google Sheets API. I need to keep all the content of the cell unchanged.
Is it possible?
Format > Number > Plain Text
If you are getting the data from the script itself, e.g, an import from a csv, you can append a single quote at the beginning to display it as is.
function forceString() {
var sheet = SpreadsheetApp.getActiveSheet();
var txt = `="THINKING"`;
sheet.getActiveCell().setValue(`'`+txt);
}
valueInputOption='RAW' fixed the problem.
The first one inserts text unchanged, the second one inserts formulas:
api.spreadsheets().values().update(
spreadsheetId=ssId, range='A1', body={'values': text},
valueInputOption='RAW').execute()
api.spreadsheets().values().update(
spreadsheetId=ssId, range='B1', body={'values': [[formula]]},
valueInputOption='USER_ENTERED').execute()

Google Sheets: How do I include a newline within a field in a local .tsv file I want to import

I know that in Google Sheets I can type Control-Return to create a sort of "phantom" return that starts a new line within a field. But what is the actual character that represents this? Obviously it's not ASCII code 13, as that is the record separator.
I would like to be able to include this mystery character in a local .tsv file which I import into Google sheets, so that these multi-line fields will display as such. Is this possible?
Thanks!
it's CHAR(34)
so something like the following in a cell would give two distinct lines of text...
="direct text input"&A3&" more text"&CHAR(34)&"Newline with new text"
Q: Google Sheets: How do I include a newline within a field in a local .tsv file I want to import
A: Surround that field with "
Update: Only when working with CSV. Google Sheets TSV doesn't seem to include the 'new lines'. More info on the link at the end.
In technical drawing class I learnt that if you don't know how to get A-->C, try to go C-->A and draw conclusions to help you achieve your goal.
If you have this CSV text file
The google sheet will look like this
Which I obtained going the other way around.
If surrounding the field with " is not possible, you may want to use this formula, which does the opposite (changes 'new line' for § ) [adapt to your needs].
# For a whole column, if there are 'new lines' in the cell,
# copy the cell changing them to '§' otherwise copy the cell 'as is'
={"description";ArrayFormula(IF( REGEXMATCH(G2:G; char(10)) ; REGEXREPLACE(G2:G;char(10);char(167));G2:G))}
More rambling here
Inspired by #MattKing's answer, an import of a *.tsv file containing this example content (note the double-quoting within the 2nd column value) ...
"example value with no line breaks" "=""line one""&CHAR(10)&""line two"""
... seems to have the desired outcome ...

Resources