Love the "=IMPORTHTML" function in google sheets. However, having an issue parsing the function in a script that I have written in Google Sheets script. The error I receive a "Formula parse error". When I enter formula within a spreadsheet with the code generated parsed in my script, the function completes as expected.
My code is as follows:
function testsetform() {
var weekno = Browser.inputBox("Enter Week No: ");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
cell.setFormula('=IMPORTHTML(ʺhttp://www.leaguesecretary.com/LeagueBowlerList.aspx?LID=40233&YearNum=2015&Season=f&WeekNum='+weekno+'"'+', ʺtableʺ, 1)';
}
The results of the parse in the cell updated appear as follows:
=IMPORTHTML(ʺhttp://www.leaguesecretary.com/LeagueBowlerList.aspx?LID=40233&YearNum=2015&Season=f&WeekNum=28", ʺtableʺ, 1)")
It appears that the results of the parse are not interpreted as desired. The format of the code looks good but obviously something wrong. I can't see it. Any ideas of what I am doing wrong would be appreciated.
I had assistance from Google support. Another pair of eyes proved invaluable. The code above had missing parenthesis and missing quote. The correct code should be:
cell.setFormula('=IMPORTHTML("http://www.leaguesecretary.com/LeagueBowlerList.aspx?LID=40233&YearNum=2015&Season=f&WeekNum='+weekno+'"'+', "table", 1)');
Related
I came across a problem with my code in Google Apps Script for Sheets. I want to get cell values from 3 different cells, concatenate them in a single string variable, and copy this string variable into a cell of my spredsheet, using the copyto function.
The problem is that Google Apps Script doesn't recognize copyto as a function, because it doesn't work with local string variables (it works fine with other function variables, such as getrange or else). Here is the part of my code that doesn't work :
var prog = f1.getRange("A3");
var jour = f1.getRange("B1");
var heure = f1.getRange("B2");
var texte = prog+" - "+jour+" à "+heure;
heure.copyTo(f2.getRange(1,2))
f1 is properly defined.
Where do I get this wrong ? Is there a workaround for this ?
Cheers
You say that f1 is properly defined and yet this example is complete and works as well.
function copy2test() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const sh2 = ss.getSheetByName('Sheet2');
sh.getRange('A1').copyTo(sh2.getRange('A2'));
}
Your example is incomplete because I cannot copy it and put it into my code editor and reproduce the same result as you get. So you haven't met the requirement for reproducibility. In my own mind I simply look at your example and assume that you don't know what you're talking about when you say it's defined properly.
You placed it in a snippet. So just run it: It returns an error with the following:
{
"message": "Uncaught ReferenceError: f1 is not defined",
"filename": "https://stacksnippets.net/js",
"lineno": 12,
"colno": 22
}
So you snippet result and I both agree. It's not defined properly.
For anyone coming across the same problem as I was, here is my solution. Because copyTo doesn't work with a string variable such as I had (or at least I don't know how to make it work), I used a different method which produces the same result.
Instead of heure.copyTo(f2.getRange(1,2))
I used f2.getRange(1,2).setValue([texte]);
which does what I wanted and work with a string variable.
I made an application with google drive and sheets api the problem is that the sheet has some formulas that has conflicts with the created api so I was forced to erase the formulas and write a little app script to autofill the formulas. I was following this [tutorial][1]. But as the formula has strings to concat it throws a syntax error. My code is here:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("I2").setFormula(CONCAT("problem string";"\n";C2;"\n";F2));
}
Do i need to declare the string as a variable?
EDIT: i am also having problems when using the \n new line character
[1]: https://www.youtube.com/watch?v=cCBtsQGtzoQ
Issues:
setFormula accepts a string but you are passing CONCAT which is not defined nor it is a built in JavaScript method. To make it a string directly, you can use template literals.
also your formula is wrong because CONCAT can concatenate only 2 strings, not 5. You need to use CONCATENATE.
Solution:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("I2").setFormula(`CONCATENATE("problem string";"\n";C2;"\n";F2)`);
}
Output:
I am using Google Sheets.
Cell A1:
=image("address.jpg")
This puts the image in a cell. To get the url from Stack Overflow produced this answer.
I created the script, and Google recognised it in autocomplete. The error I am getting is:
TypeError: Cannot call method "match" of null. (line 10).
I ran the regex through a checker, and it does get what I am looking for i.e the address, but the error seems to indicate that it's coming back with nothing.
Does this still work? Has Google changed something?
My work-around is to create two sheets and have §=image in one sheet, while in the second sheet, I remove § and use a standard Google function.
The linked solution is far better, and I'd like to implement that if I could. I cannot post a comment on the original solution's page as I don't have reputation points.
In your situation, "null" is given as an argument. Because the formula cannot be directly retrieved by =getImageUrl(A1). By this, the error of Cannot call method "match" of null. occurs. The formula can be retrieved by getFormula(). This is also mentioned at Eric Koleda's answer. So for example, the script can be modified as follows.
Modified script:
function getImageUrl(range) {
var formula = SpreadsheetApp.getActiveSheet().getRange(range).getFormula(); // Added
var regex = /=image\("(.*)"/i;
var matches = formula.match(regex);
return matches ? matches[1] : null;
}
Note:
If =image("URL") is put in "A1", when you use this like =getImageUrl("A1"). Please enclose A1 by the double quotes. By this, the string of "A1" is given to the function and is used as the range.
Reference:
getFormula()
I am a beginning Google sheets script writer, though I've done javascript and coding for years. I am trying to do some simple sorting on arrays, but can't get the UNIQUE or the SORT function to work. Any reference to them and I get:
ReferenceError: "UNIQUE" is not defined.
I'm starting to think I am missing a library or module or something needs to be enabled in sheets. I did enable the Google sheets API, through the Resources tab on the script editor.
Here is my script:
function fcnImportStockData() {
var i=1;
var arrStockSymbols = [];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
// get last cell of the range
while (sheet.getRange("C"+i).getValue() != "" ) {
sheet.getRange("d15").setValue(i);
i++;
}
arrStockSymbols = UNIQUE("C1:C"+i);
}
On a side note, I couldn't get the function ISBLANK to work either, to simplify the While loop looking for the first blank cell in the column to set the range for sorting.
I think I am missing something simple or big picture. I have scoured the web for similar situations, and found nothing much.
TIA!
First, keep in mind that Google Sheets Script is nothing more then Javascript.
UNIQUE() is not a valid JS function, but it's a spreadsheet function that can be used inside a formula.
To use it, you must set a formula into some cell:
SpreadsheetApp.getActiveSheet().getRange("X9").setFormula("=UNIQUE(C1:C" + i + ")");
When trying to parse html as xml in google apps script, this code:
var yahoo= 'http://finance.yahoo.com/q?s=aapl'
var xml = UrlFetchApp.fetch(yahoo).getContentText();
var document = XmlService.parse(xml);
will return an error like this:
Error on line 20: The entity name must immediately follow the '&' in the entity reference. (line 13, file "")
Presumably because the html is not xml-compliant in some way in line 20. What surprises me is that when you do the same thing in google sheets and also supply an xpath, the html will be parsed as xml without problems:
=IMPORTXML("http://finance.yahoo.com/q?s=aapl,"//div[#class='title']")
will return "Apple Inc. (AAPL)". I assume that the sheets function has some way of cleaning the html to make it xml compliant.
do you think that could be the case?
if yes, do you have an idea how I could adapt the xml parser in apps script in such a way that I can access html from yahoo finance and treat it as xml?
thanks in advance!
New XmlService could not do lenient parse. So no way right now. But you can still use old Xml service that is support lenient parse (perhaps IMPORTXML use it as well). The code that works:
var yahoo= 'http://finance.yahoo.com/q?s=aapl'
var xml = UrlFetchApp.fetch(yahoo).getContentText();
var document = Xml.parse(xml, true);
And there is the issue report about no ability to lenient parse in the new XmlService: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3727
So I propose you to use old way and keep an eye on this issue.