google app script map tutorial change - google-maps

Trying to use: https://developers.google.com/apps-script/articles/maps_tutorial?hl=en I receive the map correctly. However, I would like for the map to remain on one tab. The bottom of the code reads:
SpreadsheetApp.getActiveSpreadsheet().show(ui);
And I think I should change it to
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('restaurants').show(ui);
so that the map remains on one tab. But it gives me an error message. What can I do?

You are getting an error message because there is no .show() method for the Sheet Class. The ui is attached to a Spreadsheet, not a Sheet. (Comparing to Excel, Spreadsheet == Workbook, Sheet == Worksheet aka tab.)
If you want to ensure that the sheet (tab) containing the map is visible, use Sheet.activate().
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mapSheet = ss.getSheetByName("Map"); // Assuming sheet is named Map
mapSheet.activate(); // Bring Map to front
ss.show(ui); // Open UI

You have to name the tab of the spreadsheet as 'restaurants', not the spreadsheet.

Related

How to get list all sub folders name from parent folders in google sheet with apps script google drive

How can i list all sub folders name from google drive into google sheets?
Here is the code, it's working but I cant put them in to google sheets.
enter image description here
In the place of logger i need replace the google sheet. Please help me out in this scenario?
If you are planning to use custom function to write the list of sub-folder names in Google Sheets, It will not work because your function require authorization when using DriveApp it will throw an error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function as stated here.
You can write your sub-folder names in Google Sheets using Spreadsheet Service. But you need to specify the specific sheet and the cell where you want to write your data.
Sample Code:
function findSubFolder(){
var childFolder = DriveApp.getFolderById('xxxxxxxx').getFolders();
var activeSheet = SpreadsheetApp.getActiveSheet();
var activeCell = activeSheet.getActiveCell();
var childNames = [];
while (childFolder.hasNext()) {
var child = childFolder.next();
Logger.log(child.getName());
childNames.push([child.getName()]);
}
activeSheet.getRange(activeCell.getRow(),activeCell.getColumn(),childNames.length,1).setValues(childNames);
}
What it does?
Get your active sheet using SpreadsheetApp.getActiveSheet() and the active cell using Sheet.getActiveCell()
Get all your sub-folder names and add that to childNames array using Array.prototype.push()
Notice that when I pushed the child names I used [sub-folder name], This is needed when we want use Range.setValues() which accepts a 2-d array where I want to have multiple rows.
Write the childNames array to the active sheet starting from the active cell. Using Sheet.getRange(row, column, numRows, numColumns) and Range.setValues(values)
Output:
If you want to write the sub-folder names on a single cell(in your active cell), you can replace
activeSheet.getRange(activeCell.getRow(),activeCell.getColumn(),childNames.length,1).setValues(childNames);
to
activeCell.setValue(childNames.flat().toString());
Output:

Changing the active range to a newly generated sheet causing error

Receiving the error,
ReferenceError: "setActiveSelection" is not defined.
Using Google Apps Script on Google Sheets. With this function, I'm wanting to create a new sheet tab on the same workbook using a template from another workbook. Once the new sheet is created, I'd like to change the actively selected cell to cell 'B1'. The template is copying fine, but the error is coming from the line changing the active selection. I've also tried setActiveRange and even just setActiveSheet but they return the same error.
function newReception() {
var templateSpreadsheet = SpreadsheetApp.openById("ID Redacted");
var template = templateSpreadsheet.getSheets()[1];
var currentSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = template.copyTo(currentSpreadsheet).setName("Untitled
Reception");
setActiveSelection(currentSpreadsheet.getSheetbyName("Untitled
Reception").getRange('B1'));
}
Please advise on a solution. Thanks!
You're getting this issue because you're running .setActiveSelection() on its own. This method is bound to sheets, so you must run it for such. See Class Sheet documentation for more details on this method.
Try defining your sheet as a variable and then running it for your new sheet variable.
var sheet = currentSpreadsheet.getSheetbyName("Untitled Reception");
sheet.setActiveSelection('B1');

How to add Form title to data sent to a Google Sheets

I have multiple Google Forms with identical response fields inputting into a certain Google Sheets, and there is no way of telling them apart other than their names and descriptions. Using Google Apps Script, is there any way I can add the name and/or description of the Google Form into the Sheets, along with everything that is normally added to the Sheets? I did a bit of research about something with "source" but I have no idea how to implement it.
Thanks!
You can accomplish this by going to your form in edit mode selecting "Script Editor" and pasting the below code into the script. You will need to click on the "Trigger" button (looks like a clock, if you can't find it follow here https://developers.google.com/apps-script/guides/triggers/installable). Click Add Trigger and select event type "On Form Submit"
function writeFormTitle(){
//Get Form
var form = FormApp.getActiveForm();
//Get Form's Title
var formTitle = form.getTitle();
//Get the Write To Spreadsheet ID where the data will be written to
var destinationSpreadsheet = form.getDestinationId();
//Activate that Spreadsheet by the destinationSpreadsheet ID
var sheet = SpreadsheetApp.openById(destinationSpreadsheet).getActiveSheet();
//Find the last column and row
var writeToColumn = sheet.getLastColumn()+1;
var writeToRow = sheet.getLastRow();
//Write title to Spreadsheet next to submitted data.
sheet.getRange(writeToRow, writeToColumn, 1, 1).setValue(formTitle);
}

In Google script is there a way to reference a specific sheet instead of the active sheet?

Is there a way to have scripts/macros run on a specific sheet instead of the active sheet?
In my trix, i want my first tab to always be updated with the data from my calendar. The script works, but it only updates the active sheet, not the first tab. I am also trying to make a macro to copy that first tab but i can't find a way to reference the first tab, only the active sheet. Help?
function CopyAllDataTab() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
spreadsheet.duplicateActiveSheet();
};
Do you know the name of the sheet? If the sheet name stays the same, you can call the sheet by saying:
function CopyAllDataTab() {
var sheet = SpreadsheetApp.getActiveSpreadsheet.getSheetByName("Name of the sheet")
sheet.getRange('A1').activate();
sheet.duplicateActiveSheet();
};

issue with console log for google apps scripts

I'm trying to troubleshoot the below code (the main problem is that I can't get the range to display) and I've included a log command to show what is being printed out. When I run doGet() and go to View > Logs it says: "[16-10-07 17:31:19:145 EDT] number for you: Range"
sheet.getRange("H1:DE1") refers to part of a row that has dates. Shouldn't it list the contents of the row?
function doGet() {
// The code below opens a spreadsheet using its id and logs the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1aCXXXXXXXXXXXXw61H6VuigRf6YykW8O6A0WWmG46VO7ijI/edit#gid=1673918325');
Logger.log(ss.getName());
var sheet = ss.getSheets()[1];
var range = sheet.getRange("H1:DE1");
Logger.log('number for you: %s', range);
// Calling this method with "true" sets the first line to be the title of the axes
var datatable = range.getDataTable(true);
// Note that this doesn't build an EmbeddedChart, so we can't just use
// Sheet#insertChart(). If we want to do that, we should use
// sheet.newChart().addRange() instead.
var chart = Charts.newBarChart()
.setDataTable(datatable)
.setOption("title", "Your Title Here")
.build();
var app = UiApp.createApplication();
app.add(chart);
ss.show(app);
}
Can anyone advise?
You can't to print the range object in this way. Instead you have to write range.getRow() or range.getColumn(). Please note you have various get functions of the range object.
The range object does not contain sheet values. In order to get them use either of the following:
values = range.getValues(); // gets internal values
values = range.getDisplayValues(); // gets displayed values
The best approach to fix errors in an apps script is to use the built in debugger (not the Logger object, not the execution transcript) and breakpoints, like this:
At the script editors top bar, find the dropdown menu and select a function you want to debug
Set a breakpoint: click the index of a line you would like to pause at (assume to be buggy)
Click the bug icon in the top bar to run the script in debug mode up to the specified breakpoint
Inspect your variables in the opened tab
Step through the lines of your script line by line by clicking the step in, step out, and step over buttons (at the right of the script editors top bar)