Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-sheets-formula

Modifying formula to work in appsscripts code


I am looking for some help on modifying an equation so that it works inside appsscript code.

Formula in GoogleSheets: =IF(HOME!T2:T<>"",TRANSPOSE(IMPORTXML(LEFT(HOME!T2:T,FIND("#",SUBSTITUTE(HOME!T2:T,"/","#",4))-1),"//strong[@data-e2e='video-views']")),"")

Line in AppsScript: cell.setFormula("=IF(HOME!T2:T<>'',TRANSPOSE(IMPORTXML(LEFT(HOME!T2:T,FIND("#",SUBSTITUTE(HOME!T2:T,'/','#',4))-1),'//strong[@data-e2e='video-views']')),'')");

Resulting error: error in appsscript

Appreciate any help I can get with this!


Solution

  • Sorry, maybe my brain was just not functioning correctly. I have escaped the quotation marks within the String and hopefully it should work now:

    cell.setFormula("=IF(HOME!T2:T<>\"\",TRANSPOSE(IMPORTXML(LEFT(HOME!T2:T,FIND(\"#\",SUBSTITUTE(HOME!T2:T,\"/\",\"#\",4))-1),\"//strong[@data-e2e='video-views']\")),\"\")");