I am using the google spreadsheet api to update data in several spreadsheets and I need to escape some texts to use them in the following formula:
=IFNA(FILTER('Another_SheetName'!B:B, 'Another_SheetName'!A:A=92433), HYPERLINK(url, "some_text"))
where Another_SheetName
is the the name of the another sheet that I get using the spreadsheet api and some_text
a text that I want to put and I have the following two problems:
- If
some_text
have doble quotes ("
) I get a Formula parsing error. I can solve this puttingsome_text
in cell A1 and then changing the formula to=HYPERLINK(url, A1)
, but I want to know if there is a general way to escape the textsome_text
to use it inside the formula so that it is not necessary to use an additional cell. - If
Another_SheetName
can have a single quotes ('
) I get an Unsolved Sheet Name. I can fix this replacing'
by''
everytime inAnother_SheetName
, but there is another case I need to consider? Or there is a way to scape the textAnother_SheetName
to use it in a formula like=FILTER('Another_SheetName'!B:B, 'Another_SheetName'!A:A=92433)
?
Here is an example.
I update the sheet with the following code:
c = '''=IFNA(FILTER('Another' SheetName'!B:B, 'Another' SheetName'!A:A=92433), HYPERLINK(url, "some" text"))'''
batch_request = {'requests': [
{'updateCells': {
'start': {'sheetId': sheetId, 'rowIndex': i, 'columnIndex': i},
'rows': [
{'values': [{'userEnteredValue': {'numberValue': 1}}, {'userEnteredValue': {'stringValue': 'asd'}}]},
{'values': [{'userEnteredValue': {'formulaValue': c}}, {'userEnteredValue': {'formulaValue': c}}]}
],
'fields': 'userEnteredValue'
}}
]}
serviceSheet = googleapiclient.discovery.build('sheets', 'v4', credentials=credentials)
serviceSheet.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=batch_request).execute()
Thank you for your help!!
Update
I decided to detail the problem in the first question with a simpler example. The problem is that I have a text with a double quote ("
) inside and if I use the following code I get a Formula parsing error
.
url = 'https://stackoverflow.com/'
some_text = 'some "text'
c = f'=HYPERLINK("{url}", "{some_text}")'
batch_request = {'requests': [
{'updateCells': {
'start': {'sheetId': sheetId, 'rowIndex': 0, 'columnIndex': 0},
'rows': [
{'values': [{'userEnteredValue': {'formulaValue': c}}]}
],
'fields': 'userEnteredValue'
}}
]}
serviceSheet = googleapiclient.discovery.build('sheets', 'v4', credentials=credentials)
serviceSheet.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=batch_request).execute()
The way I find to solve this is problem is to insert the content of some_text
in a given cell, for example A1
, and then reference it in the above formula. Something like this:
url = 'https://stackoverflow.com/'
some_text = 'some "text'
c = f'=HYPERLINK("{url}", A1)'
batch_request = {'requests': [
{'updateCells': {
'start': {'sheetId': sheetId, 'rowIndex': 0, 'columnIndex': 0},
'rows': [
{'values': [{'userEnteredValue': {'stringValue': some_text}}, {'userEnteredValue': {'formulaValue': c}}]},
],
'fields': 'userEnteredValue'
}}
]}
serviceSheet = googleapiclient.discovery.build('sheets', 'v4', credentials=credentials)
serviceSheet.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=batch_request).execute()
but there is a way to solve this without using another cell?