Trying to duplicate a hidden template, causing it to be revealed, but then have it be hidden again after the duplication.
The hideSheet() function is not working. Any ideas?
function googleQuiz(){
//Duplicate Sheet
var ss =SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.setActiveSheet(ss.getSheetByName('QuizFormTPlate'));
ss.duplicateActiveSheet();
sheet.hideSheet();
//Rename sheet
var dSheet = ss.setActiveSheet(ss.getSheetByName('Copy of QuizFormTPlate'));
var date = new Date();
var tz = ss.getSpreadsheetTimeZone();
var marksDate = Utilities.formatDate(date, tz, 'dd-MMM');
var name = "G-Quiz ".concat(marksDate);
dSheet.setName(name);
//Insert Cell
var cell = dSheet.getRange("C2");
cell.setValue('Formative');
}
Actually it's "showSheet()"
This works for me:
var spreadsheet = SpreadsheetApp.getActive();
var sheetTemplate = spreadsheet.getSheetByName('Template');
sheetTemplate.showSheet();
//do something...
sheetTemplate.hideSheet();
Since hideSheet() takes no effect if the sheet is already hidden:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#hidesheet
Showing the hidden sheet first should do the trick:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#showsheet
Here is alternative solution that will that copy the sheet and keep it hidden
function googleQuiz(){
//Duplicate Sheet
var ss =SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('QuizFormTPlate');
var dSheet = sheet.copyTo(SpreadsheetApp.openById(ss.getId()))
dSheet.showSheet()
//Rename sheet
var date = new Date();
var tz = ss.getSpreadsheetTimeZone();
var marksDate = Utilities.formatDate(date, tz, 'dd-MMM');
var name = "G-Quiz ".concat(marksDate);
dSheet.setName(name);
//Insert Cell
var cell = dSheet.getRange("C2");
cell.setValue('Formative');
}
Basically, instead of your using duplicateActiveSheet() function of a spreadsheet object. You can use copyTo() function of a sheet object and provide it with the current spreadsheet.
sheet.copyTo(SpreadsheetApp.openById(ss.getId())).
And directly pass the new sheet object to var dSheet to make it visible and rename it.
It appears the issue is caused by using setActiveSpreadSheet on a hidden sheet, if you call ss.getSheetByName('QuizFormTPlate').showSheet(); prior to the ss.setActiveSpreadsheet(ss.getSheetByName('QuizFormTPlate')); the call to hideSheet() will work fine afterwards. Posting this here so anyone else running into this problem can just add the .showSheet() instead of reworking their whole code.
Related
I have found a script that pulls data from a cell and adds a row to the bottom of the Google Sheet, but I want to be able to have the output on a separate sheet called "Data". Here is the script:
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
var date = new Date();
var value =sheet.getRange("B9").getValue();
sheet.appendRow([date,value])
}
I know I need to change the sheet.appendRow but I don't know what I need to get it to another sheet, say in cell A2.
You must declare the second sheet.
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var date = new Date();
var value = sheet.getRange("B9").getValue();
dataSheet.appendRow([date,value])
}
See:
The Modern JavaScript Tutorial
Outputting on a different sheet
function recordValue() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Channels");
const dsh = ss.getSheetByName("Data")
dsh.getRange(dsh.getLastRow() + 1, 1, 1,2),setValues([[new Date(),sh.getRange("B9").getValue()]]);
}
setValues
I really need your help. After 2 days, I'm still on "ZERO".
I need a script that copies all the formats from an active sheet to every other sheet.
It would be nice to have the conditional formatting, alternating colors and data validation too.
I think it's not so hard, but I'm really new in script writing.
This is the sheet:
https://docs.google.com/spreadsheets/d/1mOkm_r4rngPXTkIerSQJKxRih31sM7XoZCZVnoHUc5M/edit?usp=sharing
The code so far:
enter function copyFormatting(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var copyTo = ss.getSheetByName('agent2');
var range = copyTo.getRange(1, 1, copyTo.getMaxColumns(), copyTo.getMaxRows());
sheet.getBandings()[0].copyTo(copyTo.getRange(1,1)),SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
}
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
copyFormatting(); // the function what yo like to run
}
})
}
I was able to figure out and write some functions: clear formatting on all sheets, format row headers on all sheets, show a list of sheets, but this one is too hard for me.
Even if I try to model it first with the macro recorder.
I would be very grateful, if you could help me with this issue.
I was able to figure it out:
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
var sampleSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var maxRow =sampleSheet.getMaxRows();
var maxCol =sampleSheet.getMaxColumns();
var sampleRange = sampleSheet.getRange(1,1,maxRow,maxCol);
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
var targetSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var targetRange = targetSheet.getRange(1,1);
sampleRange.copyTo(targetRange, {formatOnly:true}) // it copies the formatting
}
})
}
Reference
Copy To
Try insertSheet(options) and if that doesn't work the try copyTo(destination, options)
I'm trying to use the function copyTo(destination, copyPasteType, transposed) to copy a sheet in one file to another sheet in a different file while keeping everything, including column width, the same. Using just the function copyTo(destination), my code works and I get everything I want in a different file, EXCEPT the column width. However, when I try and use copyTo with the copyPasteType argument, the sheet is copied perfectly (with the correct column widths), BUT any subsequent code isn't run. Why is this happening, and how can I fix it?
function createSpreadsheet(foldername) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Sheet1');
var folder = DriveApp.getFolderById(foldername);
RESPONSE = FileName(); //a function that prompts the user for a filename
//creates a new spreadsheet which has two sheets, and deletes the "Copy of Sheet1" sheet
var newSpreadsheet = SpreadsheetApp.create(RESPONSE);
var newsheet = newSpreadsheet.getSheetByName('Sheet1');
var copysheet = sheet.copyTo(newSpreadsheet);
sheet.copyTo(newSpreadsheet, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
//when using CopyPasteType argument, everything from here down isn't run
copysheet.getDataRange().copyTo(newsheet.getDataRange());
copysheet.getDataRange().copyTo(newsheet.getDataRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
newSpreadsheet.deleteSheet(copysheet);
var newSpreadsheetID = newSpreadsheet.getId();
var file = DriveApp.getFileById(newSpreadsheetID);
folder.addFile(file);
DriveApp.removeFile(file);
clearYellow(); //clears contents of yellow cells
clearGray(); //clears contents of gray cells
}
I've also asked this question in Google+
I didn't find an answer as to why the copyTo with three arguments stopped the rest of my function from executing, but I found another way to copy the column widths over to the new spreadsheet that works.
function createSpreadsheet(foldername) {
var oldspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var oldsheet = oldspreadsheet.getSheetByName('Sheet1');
var folder = DriveApp.getFolderById(foldername);
RESPONSE = FileName();
//creates a new spreadsheet which has two sheets, and deletes the "Copy of Sheet1" sheet
var newSpreadsheet = SpreadsheetApp.create(RESPONSE);
var newsheet = newSpreadsheet.getSheetByName('Sheet1');
var copysheet = oldsheet.copyTo(newSpreadsheet);
copysheet.getDataRange().copyTo(newsheet.getDataRange());
var numberOfColumns = oldsheet.getLastColumn();
for (columnNumber = 1; columnNumber <= numberOfColumns; columnNumber++){
var Width = oldsheet.getColumnWidth(columnNumber);
newsheet.setColumnWidth(columnNumber, Width);
};
newSpreadsheet.deleteSheet(copysheet);
var newSpreadsheetID = newSpreadsheet.getId();
var file = DriveApp.getFileById(newSpreadsheetID);
folder.addFile(file);
DriveApp.removeFile(file);
clearYellow(); //clears contents of yellow cells
clearGray(); //clears contents of gray cells
}
I am using a script I found to copy the active sheet to all the other spreadsheets in a folder. It works fine, but I would actually like to have that copied sheet be set as the first (left-most) sheet in those other spreadsheets.
I know about "moveActiveSheet" but I can't seem to get it to work correctly (I'm still new to scripting.). The usage examples I have seen seem be for the current spreadsheet. Any help would be appreciated.
Current script:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pasteSheet = [ {name: "Paste Sheet", functionName: "copySheet"}];
ss.addMenu("Copy to Spreadsheets", pasteSheet);
}
function copySheet() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var sourceFile = DriveApp.getFileById(source.getId());
var sourceFolder = sourceFile.getParents().next();
var folderFiles = sourceFolder.getFiles();
var thisFile;
while (folderFiles.hasNext()) {
thisFile = folderFiles.next();
if (thisFile.getName() !== sourceFile.getName()){
var currentSS = SpreadsheetApp.openById(thisFile.getId());
sheet.copyTo(currentSS);
currentSS.getSheets()[currentSS.getSheets().length-1].setName('THIS WAS COPIED');
}
};
}
You just need to activate the sheet and then move it within currentSS after you set the name.
currentSS.getSheets()[currentSS.getSheets().length-1].setName('THIS WAS COPIED').activate();
currentSS.moveActiveSheet(1);
I want to focus the last row on a specific sheet (or if possible on every one of them) so here is my function triggered by onOpen on the sheet:
function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt)
sheet.setActiveRange(range)
}
This event works but if my first loaded sheet is different from the one I want it to focus on it just teleports to the other sheet. Best will be to set an .onOpen on the spreadsheet.
Is that possible?
I found a good alternative for it. It's custom menu with function link to it.
//Pierre-luc Des. Script
//Build your menu
function setMenu() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menubuttons = [ {name: "Dernière ligne usuelle", functionName: "GOTOLastRow}];
ss.addMenu("Outils", menubuttons);
}
function GOTOLastRow() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentTime = new Date();
var sheet = spsheet.getSheetByName("Usuelle_" + currentTime.getFullYear());
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);
}
Now i will have a clickable menu "Outils" which will let me do the function.
This is rather inelegant, but perhaps try just checking whether the activated sheet is the target sheet? Something like this:
function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
if (sheet == spsheet) {
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);
}
}
The Google Apps Script documentation seems to indicate that you'll need custom libraries to get any other trigger events than the ones provided, and I'm not sure if it's even in-principle possible to trigger only when a certain specific sheet is activated.
I figure checking the sheet with if comparisons is the next-simplest solution.