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);
Related
I'm a complete novice to script, so please forgive my ignorance.
I'm trying to run a script so that when a new sheet is made of a specific set of sheets I use for my clients, that new copy renames itself upon the input of a specific cell (C13) where my clients will enter their name.
I should add I was using the following code that i grabbed from a similar question on here:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var C13 = ss.getRange("C13").getValue();
var name = C13;
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet("name");
}
But this only worked for when the sheet opened, and obviously with nothing in C13 this isn't useful
Thank you
If you wish that the sheet gets renamed when a specific range gets edited than you can use onEdit() trigger.
The code below will replace the sheet's name with C13 cell's value if you change it.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = ss.getRange("C13").getValue();
if(e.range.getA1Notation() == 'C13' && name!= '') {
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(name);
}
}
I have a google apps script function that is pulling data from a spreadsheet. It is able to log the information happily and then I return the data to a javascript function. But when I try to stringify and alert the information it alerts "null".
Not sure where I am going wrong!
Javascript calling the GAS function:
google.script.run.withSuccessHandler(getNews).getTheNews();
GAS function:
function getTheNews(){
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("NewsFeed");
var theNews = ws.getRange(3, 2, ws.getLastRow()-2, 1).getValues();
Logger.log(theNews);
return theNews;
}
The GAS log:
Javascript function receiving the info:
function getNews(theNews){
var mystr = JSON.stringify(theNews);
alert(mystr);
}
Issue and workaround:
Unfortunately, it seems that in the current stage, the date object cannot be directly returned from Google Apps Script side to Javascript side. Also I could replicate your situation. I think that this is the reason of your issue. So as the current workaround, how about the following modification?
Modified script:
In this modification, the date object is converted to the string and sent to Javascript side. At Javascript side, the date string is converted to the date object.
HTML & Javascript side:
google.script.run.withSuccessHandler(getNews).getTheNews();
function getNews(theNews){
theNews = theNews.map(r => r.map(c => new Date(c))); // Added
var mystr = JSON.stringify(theNews);
alert(mystr);
}
Google Apps Script side:
function getTheNews(){
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("NewsFeed");
var theNews = ws.getRange(3, 2, ws.getLastRow()-2, 1).getValues();
return theNews.map(r => r.map(c => c.toISOString())); // Modified
}
I think that in this case, c.getTime() instead of c.toISOString() can be also used.
Note:
For example, if you want to retrieve the display values on the cells, you can also modify as follows. In this case, Javascript is not required to be modified.
From
var theNews = ws.getRange(3, 2, ws.getLastRow()-2, 1).getValues();
To
var theNews = ws.getRange(3, 2, ws.getLastRow()-2, 1).getDisplayValues();
Also I found this issue at the Google issue tracker. Ref But when I saw the status, it seems "Won't Fix (Intended behavior)".
References:
toISOString()
getDisplayValues()
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);
}
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();
}
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();