0

I've tried to get importxml into a function so that I can get a specific piece of info from a website, but the result I get is the formula not the result of the formula/function, here is my script, sorry I'm new a scripting very new. BTW if I copy and paste the value gotten from the function then I get what I'm looking for but have to "activate" the formula by adding and taking a character

function fveps2(ticker) {
 var url = `https://finviz.com/quote.ashx?t=${ticker}`;
 var Xpath_1 = '/html/body/div[4]/div/table[2]/tbody/tr[1]/td[4]';
 var importXpath_1 = '=IMPORTXML("' + url + '","'+ Xpath_1 + '")';
 return importXpath_1;
}

and the result of this script is:

=IMPORTXML("https://finviz.com/quote.ashx?t=","/html/body/div[4]/div/table[2]/tbody/tr[1]/td[4]")

rather than the above I would like to get the value of such function, so by creating a function makes it easier to call (fveps2) and get the value of such rather, than write importxml(....etc every time, therefore I can easily use my function and change the xpath to get the desired value from the source url

thanks in advance! Alex

3

1 Answer 1

0

You are apparently trying to implement fveps2() as a custom function. That will not work, because custom functions cannot set values — they can only return values. You cannot set a formula this way.

The easiest way to do this is to use a normal importxml() formula that stays the same. Make that formula refer to a cell that is changed periodically using a script that runs on a time-driven trigger.

See What are the refresh characteristics of the Google Spreadsheet Import functions? for sample code.

2
  • I'm not sure i understand your answer, I want the result of the function, I don't want to set a value, I would like to have the function work so it can return the value to the specified ticker but by creating a function makes it easier to call or get the value so rather than write importxml(....etc y can use my function and change the xpath to get the desired value from the source url, I hope I'm being clear, thanks!
    – chamuco
    Oct 7, 2022 at 5:18
  • The =importxml(...) thing is a formula. To get the result of the formula, you would have to use .setFormula() to put the formula in a cell. You cannot do that from a custom function such as your fveps2() function. To solve this in a different way, use the recipe I gave you and see the page I linked. Oct 7, 2022 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.