I'm trying to use copyTo to duplicate an entire sheet from one Google Sheet to a brand new one. Everything works great however, the newly created Sheets all say "Copy" and then the Sheet name. Is there a way to remove the word Copy? Code is below.
function setupSheet()
{
var sources = SpreadsheetApp.openById('172teYmA4Bz61PscMQiTmpqWdvBUBDu_J2hR0m_8i_b8');
var allSheets = ['Display Case', 'Your Badges', 'Badge Directory', 'Activate Sheet'];
allSheets.forEach(function(each)
{
var thisSheet = SpreadsheetApp.getActive()
var sourcevalues = sources.getSheetByName(each).copyTo(thisSheet)
});
}
var sourcevalues =
sources.getSheetByName(each).copyTo(thisSheet).setName(each);
Related
I have this script which I found here which mimicks the 'make a copy' function, but also renames the sheet all within a single action.
function onOpen() {
var menu = [{name: "Duplicate and name", functionName: "dupName"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
}
function dupName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var name = Browser.inputBox('Enter new sheet name');
ss.insertSheet(name, {template: sheet});
}
I want to take this script a step farther and use it to increase the number in the name of the previous sheet, by 1.
For example, A sheet will be called 'Sheet 1' and when I duplicate the sheet using this script, I want the new one to be called 'Sheet 2'.
Is this possible?
If so, how can I modify this script to do that?
Or is there a better way to write it in the first place?
(considering the post I'm referencing to is 4 years old).
The following script can duplicate the sheets and also rename the same with your custom name and add protection to either selected range or complete sheet as defined in the protection line:
function script() {
var spreadsheet = SpreadsheetApp.getActive();
var myValue = SpreadsheetApp.getActiveSheet().getSheetName();
spreadsheet.duplicateActiveSheet();
var totalSheets = countSheets(); //script function
myValue = "CUSTOMTEXT" + totalSheets; //sample CUSTOMTEXT1
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
var protection = spreadsheet.getActiveSheet().protect();
protection.setUnprotectedRanges([spreadsheet.getRange('C2:E5')])
.removeEditors(['user1#gmail.com', 'user2#gmail.com']);
protection.addEditor('user0#gmail.com');
SpreadsheetApp.flush();
};
function countSheets() {
return SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
}
If the same function is triggered using the doget(e) function you can be the owner of the sheet irrespective to the triggering user. Kindly follow the following for more details on it: Changing Owner of the Sheet irrespective of the duplicator
You can use this script:
function onOpen() {
var menu = [{name: "Duplicate and name", functionName: "dupName"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
}
function dupName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var array = sheet.getName().split(" ");
var ssnum = Number(array[array.length-1]);
array[array.length-1] = ++ssnum;
ss.insertSheet(array.join(' '), {template: sheet});
}
Note that you need to have a space delimiter between the text part and the number part of the sheet name in order to use this script.
Sample Spreadsheet:
I need to copy paste a range from template sheet file to current working sheet file. I understand from the below code, that is not possible.
Is it possible to achive this using Sheets API advanced service?
I actually need to copy paste cell widths, formatting, mergedcells etc from template file range to current file
I have tried with
function copyToMySheetFromTemplateFileRange()
{
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet();
var tss = SpreadsheetApp.openById(ID_OF_TEMPLATE_SPREADSHEET);
var tsheet = tss.getSheetByName("SHEET_A")
tsheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns())
var newSheet = ss.insertSheet(1);
newSheet.getRange(2, 1).activate();
tsheet.getDataRange().copyTo(newSheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
I am trying to copy a 2-page spreadsheet in Google Drive to another folder.
Here is where I've managed to bungle myself to;
function clearRange() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Internal copy');
sheet.getRange('B3:B7').clearContent();
sheet.getRange('D3:D5').clearContent();
sheet.getRange('B12:C15').clearContent();
sheet.getRange('B20:C23').clearContent();
sheet.getRange('B28:C31').clearContent();
}
function copySpreadSheet() {
var source = SpreadsheetApp.getActiveSheet();
var sourceName = source.getSheetName();
var sValues = source.getDataRange().getValues();
var destination = SpreadsheetApp.openById('1Mcnlyknqmtf2u_TEhbWxwzYqlxGw39Sno2bOXgkErb4');
source.copyTo('destination')
var destinationSheet = destination.getSheetByName('Copy of '+sourceName)
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);
function main() {
copySpreadSheet("Internal Estimate invoice Blundell");
}
Please note the first bit is another function to clear cells after saving. This is a quote template I want my shop to use without messing with the quote. I want them to input data, save it as a copy, and then reset the fields to zero, ready for the next quote.
Any help would be appreciated!
**** UPDATE ********
So, I have gotten it to work somewhat. Here is my new code;
function copySheetValuesV4(){
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheets = sourceSpreadsheet.getSheets();
var destination = SpreadsheetApp.create(sourceSpreadsheet.getName()+' Final');
for (var i = 0; i < sourceSheets.length; i++){
var sourceSheet = sourceSheets[i];
if (!sourceSheet.isSheetHidden()) {
var sourceSheetName = sourceSheet.getSheetName();
var sValues = sourceSheet.getDataRange().getValues();
sourceSheet.copyTo(destination)
var destinationSheet = destination.getSheetByName('Copy of '+sourceSheetName).setName(sourceSheetName);
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);// overwrite all formulas that the copyTo preserved */
}
destination.getSheetByName("sheet1").hideSheet() // Remove the default "sheet1" */
}
}
It makes a copy in my /root folder, which is fine for now, however it doesn't copy any of the formula, nor does it copy the protected ranges.
Any ideas?
You want to copy only the sheets which is not hidden to new Spreadsheet.
You want to keep the format and formulas when the sheets are copied.
For the destination Spreadsheet, you want to hide the sheet of sheet1.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several answers. In this answer, copySheetValuesV4() is modified.
The flow of this sample script is as follows.
Flow:
Copy the source Spreadsheet.
Delete the hidden sheets.
Pattern 1:
In this pattern, above flow is used as a simple script.
Sample script:
function copySheetValuesV4() {
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var dst = sourceSpreadsheet.copy(sourceSpreadsheet.getName()+' Final');
var sheets = dst.getSheets();
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].isSheetHidden()) {
dst.deleteSheet(sheets[i]);
}
}
dst.getSheetByName("sheet1").hideSheet();
}
Pattern 2:
In this pattern, above flow is used. And, in order to delete the history of the Spreadsheet that the hidden sheets were deleted, the temporal Spreadsheet is copied as the output Spreadsheet. Then, the new Spreadsheet is moved to the specific folder. The temporal Spreadsheet is moved to the trash box.
Sample script:
function copySheetValuesV4() {
var folderId = "###"; // Please set the destination folder ID.
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var temp = sourceSpreadsheet.copy("temp");
var sheets = temp.getSheets();
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].isSheetHidden()) {
temp.deleteSheet(sheets[i]);
}
}
temp.getSheetByName("sheet1").hideSheet();
var id = temp.copy(sourceSpreadsheet.getName()+' Final').getId();
var file = DriveApp.getFileById(id);
DriveApp.getFolderById(folderId).addFile(file);
file.getParents().next().removeFile(file);
DriveApp.getFileById(temp.getId()).setTrashed(true);
}
References:
copy(name)
deleteSheet(sheet)
Class DriveApp
If I misunderstood your question and this was not the direction you want, I apologize.
I am trying to create a Google Sheet template where a user can enter a value (Project URN) in a cell and run a script to save the template as a sheet, renamed with the Project URN.
I have created a test Google Sheet here https://docs.google.com/spreadsheets/d/1IPpQyYHl_TtNa1lGpmu4dlzwTFjOotV2OcnxFT84iUk/edit?usp=sharing
This sheet has the following script, but I cant get the renaming function to correctly work.
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("0B7RrbZuJGLlha3NvM3ZqcTY3MDA");
DriveApp.getFileById(sheet.getId())
.(makeCopy(values = SpreadsheetApp.getActiveSheet()
.getRange(2, 2).getValues()), destFolder);
} //END function saveAsSpreadsheet
I did also find the following article but wasn't able to turn it to my use. However, the secret must be here somewhere! Rename worksheet to cell value keeping format
Kitten
You almost have it. Please try this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get spreadsheet
var id = ss.getId(); // Get spreadsheet ID
var sstocopy = DriveApp.getFileById(id); //Get spreadsheet with DriveApp
var sheet = ss.getActiveSheet(); // Grab the sheet in order to find the name of the new spreadsheet to be created
var sheet_name = sheet.getRange("A1").getValue(); // Get the value, in this case: Project Urn
var folder = DriveApp.getFolderById("XXXX"); // Get the folder where the sheet needs to be placed.
sstocopy.makeCopy(sheet_name,folder); // Make the copy of the sheet in that folder.
}
Let me know if you have any questions.
I'm trying to create bidirectional synchronisation of some content between two Google Sheets. My script is working when:
the source and destination ID of the sheets are the same AND have the ID of the active spreadsheet
SO, THIS WORKS:
function myFunction() {
var sourceSpreadsheet = SpreadsheetApp.openById("ID-OF-ACTIVE-SPREADSHEET");
var destinationSpreadsheet = SpreadsheetApp.openById("ID-OF-ACTIVE-SPREADSHEET");
var sourceSheet = sourceSpreadsheet.getSheetByName("sheet1");
var destinationSheet = destinationSpreadsheet.getSheetByName("sheet2");
var sourceRange = sourceSheet.getRange("A1:A4");
// destination, columnStart, columnEnd, rowStart, rowEnd
sourceRange.copyValuesToRange(destinationSheet, 2, 2, 3, 7);
}
But when I replace the destination ID with the ID of another spreadsheet, it doesn't work.
Could it have something to do with permissions?
I have tried to publish both spreadsheets.
THIS DOESN'T:
...
var sourceSpreadsheet = SpreadsheetApp.openById("ID-OF-ACTIVE-SPREADSHEET");
var destinationSpreadsheet = SpreadsheetApp.openById("ID-OF-ANOTHER-SPREADSHEET");
...
Since I'm trying to create bidirectional sync I don't think I can use IMPORTRANGE.
Source SpreadSheet
Destination SpreadSheet
It seems like copyValuesToRange doesn't communicate outside the spreadsheet, here is a working solution:
function onChange() {
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var sourceData = sourceSpreadsheet.getRange("A1:A8").getValues();
var targetSpreadsheet = SpreadsheetApp.openById("ID-OF-ANOTHER-SPREADSHEET").getSheetByName("Sheet1");
targetSpreadsheet.getRange("A1:A8").setValues(sourceData);
}
PS: this also enables bidirectional sync between spreadsheets, just add a copy of this code on both spreadsheet A and B and replace "ID-OF-ANOTHER-SPREADSHEET" with the ID of the other spreadsheet. And then define the from- and to range in place of the "A1:A8".