cannot get value from google sheet through app script - javascript

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);
}

Related

Fill fields in template from google sheet

I am trying to use a Google Apps Script to populate fields in Google Slides from Google Sheets. I found a code online that appears to be popular to accomplish this, however, I continue to get errors that are not cited as common errors in the comments or other bodies of information.
Here is the code:
function fillTemplate() {
var Presentation_ID= "PRESENTATION URL";
var Presentation=SlidesApp.openById(Presentation_ID);
var values= SpreadsheetApp.getActive().getDataRange().getValues();
values.forEach(function(row){
var templateVariable= row[0];
var templateValue= row[1];
Presentation.replaceAllText(templateVariable, templateValue);
});
}
I have attempted to populate the sheet URL and the range as follows:
var values= SpreadsheetApp.getActive(SHEET URL).getDataRange(FIRSTCELL:LASTCELL).getValues();
The error message is as follows:
Execution log
11:04:27 AM Notice Execution Started.
11:04:30 AM Error. Exception: The match text should not be empty
(anonymous) # Code.gs:10
fillTemplate # Code.gs:7
For what it's worth, my cell range is C49:D60, but I have tried to move it to a separate sheet altogether with A1:B12

Renaming Google Sheets File Automatically From A Cell

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);
}
}

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);

Sheet name is not updating with the script

In my google spreadsheet i created a script (below) which give me the function/formula to get the name of the sheet/tab which i am currently in.
The problem is when i change the sheet name to a new name then this cell is not updating with the new name even after several refresh.
I have set triggers to run the script as well as i have used SpreadsheetApp.flush() to forcefully applies all pending Spreadsheet changes, but still this does not work.
This is my script below:
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
return s.getName();
}
And this is with the flush :
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
SpreadsheetApp.flush();
return s.getName();
}
Please let me know what changes i need to make so that this can update as and when the sheet name is changed.
This function works when connected to an onChange trigger and the Sheet Name is changed. I think you were adding it to the cell as "=sheetName()". I have to admit I don't know much about custom functions because I always do all of my calculations in arrays because it's much faster. But if you hook this up to an onChange event and set the load value of the sheet name into a cell it works.
function sheetName()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sht=ss.getActiveSheet();
var name=sht.getName();
sht.getRange('A5').setValue(name);
}

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