Google Sheets Programs on Mobile App - javascript

I have the following code:
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var task = ss.getRange(2,4).getValue(); //ss.getRange("A2").getValue();
var who = Session.getActiveUser();
var date = new Date();
function start() {
var last = ss.getLastRow();
ss.getRange(last+1, 1).setValue(task);
ss.getRange(last+1, 2).setValue(who);
ss.getRange(last+1, 3).setValue(date);
}
function end() {
var last = ss.getLastRow();
ss.getRange(last, 4).setValue(date);
ss.getRange(1,1).setValue(date);
ss.getRange(1,2).setValue(ss.getRange(last+1, 3));
}
in a Google Sheet script. It works on desktop - basically what it does is it will track when a user starts and stops a given task.
The sheet shows up fine in the Sheets app, but the script doesn't run. What needs to happen so that it will run? Is there some way to run the programs on the app?

Related

I need 2 different buttons, to create new folder and create a new PDF in that folder

I've been struggling to build a specific weekly stock system reports. So to give you a basic overview, I have a mastersheet that I want to generate reports from, triggered by an UI button. The first step however is to create a folder for that week to place the PDF's in. I can create the folder, and I can generate the PDF in my root Google Drive folder, but I can't seem to move the PDF anywhere after that. I have attempted to use .moveTo() but I can't get that to work. Does anyone have any advise?
function onOpen(e)
{
SpreadsheetApp.getUi()
.createMenu('Physical')
.addItem('New folder','newFolder')
.addItem('Generate PDF','generatePDF')
.addToUi();
}
function newFolder(){
var today = new Date();
var week = Utilities.formatDate(today, "Europe/Amsterdam", "w"); //need to find a way to minus 1 for the current week
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId(); //time to create a new folder
var spreadsheetFile = DriveApp.getFileById(spreadsheetId);
var folderId = spreadsheetFile.getParents().next().getId();
var parFolder = DriveApp.getFolderById(folderId)
var destFolder = parFolder.createFolder('Week ' + week);
}
function generatePDF(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var speadsheetFile = ss.getId();
var file = DriveApp.getFileById(speadsheetFile);
var folderId = file.getParents().next().getId();
var pdf = DriveApp.createFile(ss.getBlob())
pdf.moveTo(folderId); //find way to move file either to destination folder or to parent folder
}
Description
These types of situations are hard to test because the circumstances are specific to the OP questioner. However, I believe this will work.
Using the PropertyService Script Properties, store the newly created folderId and then get that id from Script Properties to move the file.
A note of caution, I didn't check for the case if the week changes and a new folder is not created, the pdf will go to the previous week folder.
Regarding creating a button and linking a function to the button see this article Buttons in Google Sheets
Script
function newFolder(){
var today = new Date();
var week = Utilities.formatDate(today, "Europe/Amsterdam", "w"); //need to find a way to minus 1 for the current week
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId(); //time to create a new folder
var spreadsheetFile = DriveApp.getFileById(spreadsheetId);
var folderId = spreadsheetFile.getParents().next().getId();
var parFolder = DriveApp.getFolderById(folderId);
var folderName = 'Week '+week;
// check if folder already exists
var subFolders = parFolder.getFoldersByName(folderName);
var destFolder = null;
if( subFolders.hasNext() ) {
SpreadsheetApp.getUi().alert("Folder "+folderName+" already exists");
destFolder = subFolders.next();
}
else {
destFolder = parFolder.createFolder(folderName);
}
// store folder id to Script Properties
var props = PropertiesService.getScriptProperties();
props.setProperty("foldeId",destFolder.getId());
}
function generatePDF(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get folder id from Script Properties
var folderId = PropertiesService.getScriptProperties("folderId");
if( !folderId ) {
SpreadsheetApp.getUi().alert("Property folderId not found");
return;
}
var pdf = DriveApp.createFile(ss.getBlob())
pdf.moveTo(folderId); //find way to move file either to destination folder or to parent folder
}
Reference
SpreadsheetApp.getUi().alert()
PropetiesService

Pass variable between two functions

I want to write a app which generates a spreadsheet in my google-drive with the user input data with google-app scripts.
For doing so I have several JavaScript-functions I want to execute one after the other:
function createSpreadsheet(name){
var ss=SpreadsheetApp.create(name);
var ssID = ss.getID()
}
function writeData(data){
var s = ss.getSheets()[0];
s.getRange('A1').setValue(data);
}
which are called by the frond-end script via:
<script>
function parse_to_backend(){
var name = document.getElementById("user_name").value;
var data = document.getElementById("user_data").value;
google.script.run.createSpreadsheet(name);
google.script.run.writeData(data);
};
</script>
how can I achieve that the writeData knows ssID the ID of the spreadsheet (I can not return values to the frontend with google.script.run and parse it as an argument)?
You want to create new Spreadsheet from HTML side.
You want to put the value to the created Spreadsheet from HTML side.
In your situation, you don't want to put writeData() in createSpreadsheet(). Namely, you want to individually use both functions.
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
At first, the modification point is as follows.
Modification point:
When you run the script as follows,
google.script.run.createSpreadsheet(name);
google.script.run.writeData(data);
Function of writeData() is run before the function of createSpreadsheet() is finished, because google.script.run works by the asynchronous process. In order to avoid this, in this sample script, withSuccessHandler() is used.
About the modified script, I introduce 2 patterns. Please select one of them.
Pattern 1:
In this pattern, file ID is returned from createSpreadsheet() and the value is put to the created Spreadsheet using the file ID.
Code.gs: Google Apps Script
function createSpreadsheet(name){
var ss = SpreadsheetApp.create(name);
var ssID = ss.getId();
return ssID;
}
function writeData(id, data){
var ss = SpreadsheetApp.openById(id);
var s = ss.getSheets()[0];
s.getRange('A1').setValue(data);
}
index.html: HTML and Javascript
<script>
function parse_to_backend(){
var name = document.getElementById("user_name").value;
var data = document.getElementById("user_data").value;
google.script.run.withSuccessHandler((id) => {
google.script.run.writeData(id, data);
}).createSpreadsheet(name);
};
</script>
Pattern 2:
In this pattern, file ID is saved by PropertiesService and the value is put to the created Spreadsheet using the file ID got from PropertiesService. In this case, the file ID is saved to PropertiesService. So the created Spreadsheet can be also used by other action of Javascript using the file ID got from PropertiesService.
Code.gs: Google Apps Script
function createSpreadsheet(name){
var ss = SpreadsheetApp.create(name);
var ssID = ss.getId();
PropertiesService.getScriptProperties().setProperty("ssID", ssID);
}
function writeData(data){
var id = PropertiesService.getScriptProperties().getProperty("ssID");
var ss = SpreadsheetApp.openById(id);
var s = ss.getSheets()[0];
s.getRange('A1').setValue(data);
}
index.html: HTML and Javascript
<script>
function parse_to_backend(){
var name = document.getElementById("user_name").value;
var data = document.getElementById("user_data").value;
google.script.run.withSuccessHandler(() => {
google.script.run.writeData(data);
}).createSpreadsheet(name);
};
</script>
References:
Class google.script.run
withSuccessHandler()
Class PropertiesService
If I misunderstood your question and this answer was not what you want, I apologize.

Automatic Google Sheets Back Up without Copying the Forms attached to the Sheets

How do I make a script that copies a Google Sheet as a backup into a backup folder in the Google Drive, but not making copies of the Google Forms that are attached to the Sheet?
function backUp() {
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;
var destination = DriveApp.getFolderById("1NNhZESRq_0_DVBwzA8fvxSYGMpOa6KC_");
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
file.makeCopy(name, destination);
}
This one automatically copies the original files into the Backup Folder, but it also copies the Google Forms that are linked to it... so I end up with 10+ copies every day after it saves.
Great question. It was little hard for my knowledge, I catch it :)
Is unable to copy whole Spreadsheet without Form, because they are magically connected (magically ==> connection is under any public API possibilities).
You can take to use the copy sheet to another spreadsheed feature.
I tried it and it doesn't create copy of Form, yeah!
But for this case have Script & Drive unwieldy API. Result looks too hacky:
function backUp() {
//Open old spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
//Prepare new name
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = spreadsheet.getName() + " Copy " + formattedDate;
//Prepare file system objects
var oldfile = DriveApp.getFileById(spreadsheet.getId());
var destination = DriveApp.getFolderById("1NNhZESRq_0_DVBwzA8fvxSYGMpOa6KC_");
//create new spreadsheet
var newSpreadsheet = SpreadsheetApp.create(name);
//move to destination folder (yep, it's hacky)
var newFile = DriveApp.getFileById(newSpreadsheet.getId());
var parents = newFile.getParents();
while (parents.hasNext()) {
var parent = parents.next();
parent.removeFile(newFile); //remove from default folder
}
destination.addFile(newFile);
//copy all sheets to new spreadsheet
var sheets = spreadsheet.getSheets();
sheets.forEach(function(sheet) {
sheet.copyTo(newSpreadsheet);
});
//remove empty sheet (created with new spreadsheet)
newSpreadsheet.deleteSheet(newSpreadsheet.getSheets()[0]);
}

Run a Google Apps Script from Chrome Extension

I'm new to Google Chrome Extension development and I want to run an apps script from the extension when I click the button from extension. But I'm not sure how to do it and I've checked the guide but couldn't catch the idea. Here are my codes,
script.js
$(document).ready(function () {
$('#btnGet').click(function () {
$(document).load('https://script.google.com/macros/s/AKfycbwpkBW7qKZBeJ011C7j3le4vV8D0SEHu9709mWtEMzJgrJmHnaR/exec');
});
});
Apps Script
function doGet(e){
function newEntry() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lr = sheet.getLastRow();
sheet.getRange(lr+1, 1, 1, 3).setValues([["First Name", "Last Name", "Email"]]);
Browser.msgBox("New Entry Added!");
}
}
I've managed to run codes from my script.js but I'm not sure how can I run codes from my apps script. How can I run the codes from my apps script?
Short answer
Instead of
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
try
var id = 'spreadsheet-id'; //replace with your spreadsheet id
var sheetName = 'sheet name'; //replace with your sheet name.
var ss = SpreadsheetApp.openByID(id);
var sheet = ss.getSheetbyName(sheetName);
Explanation
The methods that return spreadsheet active objects can be used when the script is being called from the spreadsheet. doGet(e) could interact with a spreadsheet but you should specify which is the one that you want to work on.
References
openByID(id)
getSheetByName(name)

Google Scripts - How to call the correct sheet

I´m learning how to use the Google Scripts, I saw a video on the youtube and I wrote this code based on the video. It will be a dashboard, but the problem I have is: In this spreadsheet, I have more than one Sheet. How can I "tell" the code which sheet it would have to check? In my case the Sheet ID is 8.
I think this would be the only "mistake" in the code!
Thanks for helping!
function doGet() {
// Identify the spreadsheet where the data is stored.
var ss = SpreadsheetApp.openById('0AjizIuAuEzaydFlsRDRrTkZSekROaWNZNV9QbjRDdUE')
var data = ss.getDataRange();
// Create all the filters
var clienteFilter = Charts.newNumberRangeFilter().setFilterColumnIndex(1).build();
var pedidoFilter = Charts.newNumberRangeFilter().setFilterColumnIndex(2).build();
var necessidadeFilter = Charts.newCategoryFilter().setFilterColumnIndex(3).build();
var entregaFilter = Charts.newCategoryFilter().setFilterColumnIndex(4).build();
var notaconsultorFilter = Charts.newNumberRangeFilter().setFilterColumnIndex(5).build();
var notaentregadorFilter = Charts.newNumberRangeFilter().setFilterColumnIndex(6).build();
var recomendariaFilter = Charts.newCategoryFilter().setFilterColumnIndex(8).build();
var atencaoFilter = Charts.newCategoryFilter().setFilterColumnIndex(9).build();
var servicosFilter = Charts.newCategoryFilter().setFilterColumnIndex(10).build();
var entregadorFilter = Charts.newCategoryFilter().setFilterColumnIndex(11).build();
// Create all the charts
var pieChart = Charts.newPieChart()
.setDataViewDefinition(Charts.newDataViewDefinition().setColumns([11,6]))
.build();
// Create the dashboard
var dashboard = Charts.newDashboardPanel().setDataTable(data)
.bind([clienteFilter, pedidoFilter, necessidadeFilter, entregaFilter, notaconsultorFilter, notaentregadorFilter, recomendariaFilter, atencaoFilter, servicosFilter, entregadorFilter],[pieChart])
.build();
// Create the webapp and bind together the filters and charts
var app = UiApp.createApplication();
var filterPanel = app.createVerticalPanel();
var chartPanel = app.createHorizontalPanel();
filterPanel.add(clienteFilter).add(pedidoFilter).add(necessidadeFilter).add(entregaFilter).add(notaconsultorFilter).add(notaentregadorFilter).add(recomendariaFilter).add(atencaoFilter).add(servicosFilter).add(entregadorFilter).setSpacing(10);
chartPanel.add(pieChart).setSpacing(10);
// Format the dashboard layout
dashboard.add(app.createVerticalPanel().add(filterPanel).add(chartPanel));
app.add(dashboard);
return app;
}
in your code , ss comes for a spreadSheet object, which is -way of speaking - the "parent" of the included sheets.
This class (spreadsheet object) has a couple of methods to get sheets that are described in the documentation, for example getSheetByName('name of the sheet') or getSheets() which returns an array of sheet objects. In this case you can chose which one you use with getSheets()[number]

Categories

Resources