'Missing ; before statement. (line 2, file "Code")' error - javascript

I am trying to create a chart dashboard from a Google sheet, I have used the tutorial but am getting the following error on line 2:
'Missing ; before statement. (line 2, file "Code")'
The start of my code runs like so:
function doGet() {
vars ss = SpreadsheetApp.openById(1POyMyUMGefIgt-I8TFgQEwN4XHH5nFgNr7BAnY39-Y4);
vars data = ss.getDataRange()
var organisationFilter = Charts.newCategoryFilter().setFilterColumnIndex(0).build()
var templateFilter = Charts.newCategoryFilter().setFilterColumnIndex(1).build()
Any ideas why I cant run this script?
Thanks,

First, what is vars? I hope that it is a typo. That's why you get a error.
If you want to declare a variable, you should use var just like two lines below your vars.
Second, you don't have semicolons in the end of your code lines.
As far as I know, it is not obligatory. However, it is highly recommended.
Third, why isn't your string arounded by quotes?
Your code should look like:
function doGet()
{
var ss = SpreadsheetApp.openById('1POyMyUMGefIgt-I8TFgQEwN4XHH5nFgNr7BAnY39-Y4');
var data = ss.getDataRange();
var organisationFilter = Charts.newCategoryFilter().setFilterColumnIndex(0).build();
var templateFilter = Charts.newCategoryFilter().setFilterColumnIndex(1).build();
}

Related

Processing lines and filling fields

I read and search and study, but can't seem to write any JS out of my own head - this should be really simple but it will not work. I keep getting an error about an undefined variable "madlib7.html:411 Uncaught ReferenceError: populatefields is not defined at HTMLButtonElement.onclick"
I start with some urls that I paste into a textarea, then I click a button and first it splits the text into individual lines and stores them in variables. Next, I want to split each line by spaces and populate fields in an HTML form with select words from the resulting array via the use of innerhtml.
I know there is some way of looping through the processes, but I have not figured out how to do that yet so for now I am writing it all out the long way. I have not been able to figure out how to address the error message 'ReferenceError' from above.
Thanks for any and all help
Paul
populatefields(day1, location1, streamid1) {
document.getElementById('PanLinks').value.split('\n');
var streamInfoLine1 = resultArr[0];
var streamInfoLine2 = resultArr[1];
var streamInfoLine3 = resultArr[2];
var streamInfoLine1 = resultArr[3];
var streamInfoLine2 = resultArr[4];
var streamInfoLine3 = resultArr[5];
var streamInfoLine1 = resultArr[6];
var streamInfoLine2 = resultArr[7];
var streamInfoLine3 = resultArr[8];
streamInfoLine1.split(' ');
var day1 = resultArr[0];
var location1 = resultArr[3];
var streamID1 = resultArr[4];
document.getElementById("location1").innerHTML= location1;
document.getElementById("time1").innerHTML= day1;
document.getElementById("streamid1").innerHTML= streamID1;
}
could you use stackoverflow built in code sandbox to include all your code in html, javascript code? it seems like the issue is on your html file madlib7.html, where your onclick listener isn't defined on the button. nor did I see anything from your code that indicates any event listener has been setup to perform that button task. so it's hard to help debug what exactly it is.

Save spreadsheet row data to PDF using Gdoc template using Google Apps Script

I've exhausted all resources I'm aware of. Watched several different tutorials and spent many hours trying to make this code work.
I'm trying to automatically create a PDF from a spreadsheet row. I save copies of code at every successful run. However, I can't figure out at which point i'm triggering this error:
TypeError: Cannot read property 'makeCopy' of undefined (line 35, file "CreatePDF")
I was able to run the CreatePDF function successfully when defining the variables with "let" but since trying to pull them from the spreadsheet with the function: createbulkpdfs I haven't been able to progress.
Can anyone point me in the right direction? Line 35 noted below**
//doc id 1rFkh5AmLPNAJ7z1olWBvubn-6Oh8S8_my-AML1lU48I
//temp folder 12ZEXitbgdyOQJiC-pHvshgsBuNOKEKpj
//pdf folder 1eomwcc_UNJhFhvxcoWwamyJUterZtTaB
//let name = "Dan";
//let email = "daniel.sgalia#gmail.com";
//let cost = "100";
//let squarefeet = "2000";
//let flooringmaterial = "Wood";
//let roofingmaterial = "Standing Seam";
//let sidingmaterial = "Lap";
//let kitchencounter = "Granite";
function createbulkpdfs(){
const docfile = DriveApp.getFileById("1rFkh5AmLPNAJ7z1olWBvubn-6Oh8S8_my-AML1lU48I");
const tempfolder = DriveApp.getFolderById("12ZEXitbgdyOQJiC-pHvshgsBuNOKEKpj");
const pdffolder = DriveApp.getFolderById("1eomwcc_UNJhFhvxcoWwamyJUterZtTaB");
const currentsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SFR Calculations");
const data = currentsheet.getRange(2, 1,currentsheet.getLastRow()-1,15).getValues();
data.forEach(row => {
createPDF(row[0],email,cost,squarefeet,flooringmaterial,roofingmaterial,sidingmaterial,kitchencounter,pdfname,docfile,tempfolder,pdffolder)
});
}
function createPDF(name,email,cost,squarefeet,flooringmaterial,roofingmaterial,sidingmaterial,kitchencounter,pdfname,docfile,tempfolder,pdffolder) {
**LINE 35** const tempfile = docfile.makeCopy(tempfolder);
const tempdocfile =DocumentApp.openById(tempfile.getId());
const body = tempdocfile.getBody();
body.replaceText("{Name}", name);
body.replaceText("{Email}", email);
body.replaceText("{Cost Estimate}", cost);
body.replaceText("{Square Feet}", squarefeet);
body.replaceText("{Flooring Material}", flooringmaterial);
body.replaceText("{Roofing Material}", roofingmaterial);
body.replaceText("{Siding Material}", sidingmaterial);
body.replaceText("{Kitchen Counter}", kitchencounter);
tempdocfile.saveAndClose();
const pdfcontentblob = tempfile.getAs(MimeType.PDF);
pdffolder.createFile(pdfcontentblob).setName("pdfname");
tempfolder.removeFile(tempfile)
}
Not sure if you worked this out already, but I think the solution might be super simple...
Before you run the script, change it so you are running the function 'CreateBulkPDFs' not 'Create PDF'.
You have two functions in your script, you need to use the drop down at the top next to the Run button to ensure you are running the function 'CreateBulkPDFs'.
If you try to run 'Create PDF' the consts are not defined.
That error would throw because something isn't defined properly. At a glance it looks like because on line 35 you reference 'docfile' and 'tempfolder' which are not defined in the createPDF() function. (They are defined in the previous function)
It's not clear to be why you have the two functions and why you need createPDF to reference the other one, but it appears if you defined 'docfile' and 'tempfolder' (or any var or const that is needed in that function) within createPDF() specifically then it should run.
This should technically answer you question of why the error is happening on that line currently, without going into the full scope of needs for your project.

cannot get value from google sheet through app script

I'm starting to learn how to code with Google App script and just completed the codelab playlist on Extending Google Sheets.
So to try it out I wrote a code with the intention of pulling a value from a specific cell from a sheet through a variable calles "OrderNum", using Logger to log the variable that would contain this value to check my code, but for some reason I keep getting an "(variable) is not defined)" error and I've tried multiple things already and nothing works... what I am doing wrong?? Here is my code:
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Create a custom menu
ui.createMenu('Order Functions')
.addItem('Copy form to database',"copy2db")
.addToUi();
}
// Pull orderNum value from sheet
function checkOrderNum() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var orderNum = sheet.getRange('H2').getValue();
}
Logger.log('var ', orderNum);
snippet of google sheet showing value of cell H2
The error I'm getting is "ReferenceError: orderNum is not define). I don't get it, it seems to me that all the references are there. Also when I remove the logger command, no error appears but then I have no idea if my code is correct, which apparently isn't.
Thanks for the help,
Nestor
You are getting an error message : "ReferenceError: orderNum is not define).
This is because your Logger statement is not part of a function.
Logger is a feature of Google scripts and Logger.log is well documented.
In the following example, I have included three versions of a Logger statement.
Logger.log("the order number is "+orderNum);: in this case the description text is in quotes, followed by the "+" operator and the variable name.
Logger.log('var %s', orderNum);: the suggestion made by #Tanaike. In the case, '%s' acts as a placeholder doc ref.
Logger.log('var ', orderNum);: this is your logger statement, but this time it is included within the function.
These statements return these results:
the order number is 1001
var 1001.0
var 1001.0
// Pull orderNum value from sheet
function checkOrderNum() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var orderNum = sheet.getRange('H2').getValue();
Logger.log("the order number is "+orderNum);
Logger.log('var %s', orderNum);
Logger.log('var ', orderNum);
}

ReferenceError: "sheet" is not defined

It is my first time to develop the google script for my google sheet
Here is the code of my script
function doGet(e) {
var url = 'https://docs.google.com/spreadsheets/d/1p3fW0Vkx89tdiws3Z-a5_LpLwyGYj7pacIek3f9Z96w/edit#gid=1862590156'
var name = '無標題表單 (回應)'
var SpreadSheet = SpreadsheetApp.openByUrl(url);
var values = sheet.getSheetValues(1, 1, 1, 1);
Logger.log(values);
}
When I run the script,and then it show the message "ReferenceError: "sheet" is not defined. (第 7 行,檔案名稱:巨集)"
I try to use the method in this article ReferenceError: "Sheets" is not defined but still cannot work.
Thank you!
tl;dr
Change var SpreadSheet to var sheet in the provided code.
As the error message is explaining: The variable sheet is not defined in the provided code.
This means at the point where sheet is trying to be used, it does not exist yet. It will have to be defined first.
Try running one of the following before you use the variable sheet
var sheet = SpreadSheet.getSheetByName('YourSheetName');
or using .openByUrl
var sheet = SpreadsheetApp.openByUrl(url);

Accessing spreadsheet in Google Script

I am writing a google script on top of a spreadsheet.
I want to deploy it as a web app.
It shows some values.
Unfortunately, with my current code, google reminds me:
TypeError: Cannot call method "getSheetByName" of null.
I have no idea where the mistake is.
Here's the code
function doGet(e) {
var app = UiApp.createApplication().setTitle('Details');
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var dataFromCell = ss.getRange("B4").getValue();
var mypanel = app.createVerticalPanel();
var label = app.createLabel(dataFromCell);
app.add(mypanel);
app.add(label);
return app;
}
In standalone webapps you cannot use getActiveSpreadsheet because no one is using it actively...
Use SpreadsheetApp.openById('ID') instead, you can get the ID in the url of your spreadsheet like in this example :
https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3d-------nZIV0JQQ0c1a3dWX1dQbGc#gid=0
between key= and #, ie 0AnqSFd3iikE3d-------nZIV0JQQ0c1a3dWX1dQbGc
Not need to use ID , just try this code ( change mygooglelocation with your Spreadsheet name and range of cells. Working very well for me with google maps...
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('mygooglelocation');
var ss = SpreadsheetApp.getActive();
var mylocationInfo = ss.getRange("A2:B4").getValues();

Categories

Resources