I am copying some data from a sheet on one spreadsheet to a sheet on a separate spreadsheet. I want this function to run everyday, but I only want it to run on the sheets that the user has manually run it on. There are other sheets in the source Spreadsheet that have private information that cannot be shared on the destination Spreadsheet.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('My Menu')
.addItem('Create Read-Only', 'copyInfo')
.addToUi();
}
// Copies all info to a new sheet in Check Submission
function copyInfo() {
var copySheet = SpreadsheetApp.getActiveSheet();
var sheetName=copySheet.getName();
var pasteSpreadsheet = SpreadsheetApp.openById("1NXmdq6yCKC6oiDAQNXcovj85697__rCeVJiIT_ou_gI")
//Create new sheet if one doesn't exist
var newSheet = pasteSpreadsheet.getSheetByName(sheetName);
if (newSheet != null)
pasteSpreadsheet.deleteSheet(newSheet);
newSheet = pasteSpreadsheet.insertSheet();
newSheet.setName(sheetName);
//Copy and paste data
// Clear the Google Sheet before copy
newSheet.clear({contentsOnly: true});
// get source range
var source = copySheet.getRange("B2:B");
var values = source.getValues();
// get destination range
var destination = newSheet.getRange(1,1,values.length,values[0].length);
// copy values to destination range
destination.setValues(values)
}
Related
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
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]);
}
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?
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)
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]