-1

I tried to create a Google Scholar function in Google Sheets that allows you to run Google Scholar searches using predefined search keys and return the number of hits/search results through Google Appscript. However, when I run th script have been encountering the following error:

"TypeError: Cannot read property 'replace' of undefined",

Any idea what I can do to the script to get around the error?

/**
* Returns the "about n results from google scholar"
*
* @param {string} the search query.
* @return {number} the about amount.
* @customfunction
*/
function SCHOLAR(query){
query = query.replace(" ", "+");
const url = `https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=${query}&btnG=`;
const req = UrlFetchApp.fetch(url)
const html = req.getContentText()
const amountText = /<div class="gs\_ab\_mdw">About (.*?) results/gmsi.exec(html)[1]
const amount = Number(amountText.replace(",",""))
return amount
}
  • The list of available functions is on the official help. It could be reached from the help menu and form the help center -> https://support.google.com/docs/table/25273?hl=en. Regarding using Google Apps Script, please show what you have tried and add a brief description of your search efforts as is suggested in [ask]. – Rubén May 15 '22 at 17:04
  • How are you calling this function? It should be called from the sheet as a custom function: `=scholar(" your query")` – TheMaster May 16 '22 at 06:13
  • Yes that is how I wanted to call the function. My intention is to use Google sheet formulas (ex. concatenate) to create the search key boolean statements ("name x" OR "name y"). The search key statements will then go in the "query" part of the function. (Ex. =Scholar("name x" OR "name y")). This will hopefully get inserted into the Google scholar search html and allow me to pull the number of search results generated back into my spreadsheet. – Jonathan Poon May 17 '22 at 00:20
  • The error you quoted says you are calling the custom function from the script editor without `query`. You should quote the proper error, When running it as a custom function. See [related answer](https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas/63851123#63851123) – TheMaster May 17 '22 at 07:28
  • Hi, thank you for pointing me in the direction. I called the custom function ( = scholar("Trucks")) and in the app script execution log I get the following error: "TypeError: Cannot read property '1' of null at SCHOLAR(Code:13:81)" In the actual google spreadsheet: "TypeError: Cannot read property '1' of null (line 13)." Based on the message I looked back at line 13 of the script which is: "const amountText = /
    About (.*?) results/gmsi.exec(html)[1]" Not quite sure what I should modify to make this work. Thanks!
    – Jonathan Poon May 28 '22 at 16:25
  • You should examine the ``html`` you fetched and see where it has the information you're looking for and then use regex(https://regex101.com) to correctly get that information. Alternatively use "cheerios for GAS" instead of regex. – TheMaster May 28 '22 at 20:12

0 Answers0