I am stuck here. I would like to create a sheet with a name of a given cell in my main sheet (this part works). Then, I would like to copy the values from cells G4:I26 into the new sheet that was just created (not working). Here is what I have so far:
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getRange("J1").getDisplayValue();
ss.insertSheet(sheetName, ss.getSheets().length);
var source = ss.getRange('G4:I26');
source.copyTo(sheetName.getRange('A1'), {contentsOnly: true});
source.clear();
Try the following script code:
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var newSheetName = ss.getRange('J1').getDisplayValue();
var newSheet = ss.insertSheet(newSheetName, ss.getSheets().length);
var source = sheet.getRange('G4:I26');
source.copyTo(newSheet.getRange('A1'), {contentsOnly: true});
source.clearContent();
};
Change the Sheet name "Sheet1" in the above code with the sheet's name in which you have the cell "J1" containing the new sheet's name.
Related
I have a Google-Sheet script that creates few cells and inserts them into active sheet. I would like to ask someone for help. I would need the script to do exactly same work, but only change I would need is to pick the sheet where the cells will be added. At this moment it always applies to the active sheet.
As you may recognized I am absolutely noob in coding and also in English, so I really appriciate everyone who tries to help me with this.
Thank you very much, Petr
The code I have right now:
function myButton(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
How about the following modification patterns?
Pattern 1:
When you want to select the specific sheet in the active Spreadsheet, you can modify your script as follows.
function myButton() {
var sheetName = "Sheet2"; // Added. Please set the sheet name you want to select.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName); // Modified
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
Pattern 2:
When you want to select the specific sheet in the other Spreadsheet from the active Spreadsheet, you can modify your script as follows.
function myButton() {
var spreadsheetId = "###"; // Added. Please set the spreadsheet ID you want to use.
var sheetName = "Sheet2"; // Added. Please set the sheet name you want to select.
var ss = SpreadsheetApp.openById(spreadsheetId); // Modified
var sheet = ss.getSheetByName(sheetName); // Modified
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
Note:
In this answer, from I would need the script to do exactly same work, but only change I would need is to pick the sheet where the cells will be added. At this moment it always applies to the active sheet., it supposes that your script works fine for the 1st tab in the active Spreadsheet. Please be careful this.
When getSheets() is used, for example, in the case of var sheet = ss.getSheets()[0];, it's the 1st tab. In the case of var sheet = ss.getSheets()[1];, it's the 2nd tab.
References:
getSheetByName(name)
openById(id)
getSheets()
i have made this in excel but cannot use it online. so i made it in google sheets but it does not work. What i want to do:
data ( values only, no formats) from sheet Tmrw range C3:Y20 copy to sheet Today range C3:y20, and after that clear data from sheet Tmrw range C3:Y20
i used this code:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Tmrw"!C3:Y20");
var destSheet = ss.getSheetByName("Today");
var destRange = destSheet.getRange("Today!C3:Y20");
source.copyTo (destRange,"Today!C3:Y20);
source.clear ();
}
it does not work. i am not surprised.
the file is here:
https://docs.google.com/spreadsheets/d/11uyaRe7khP9PywXKkEh5e5xWLvm68Nw58R6dHkaTxvk/edit?usp=sharing
Modification points:
When getRange of Class Spreadsheet, you can include the sheet name to the A1Notation.
In this case, ss.getRange("'Today'!C3:Y20") can be used instead of ss.getSheetByName("Today");.
"Tmrw"!C3:Y20" and "Today!C3:Y20 are not enclosed by ". In this case, please modify to "Tmrw!C3:Y20" and "'Tmrw'!C3:Y20", and "Today!C3:Y20" and "'Today'!C3:Y20".
In your situation, I thought that the arguments of copyTo of Class Range is copyTo(destination).
When above points are reflected to your script, it becomes as follows.
Modified script:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange("'Tmrw'!C3:Y20");
var destRange = ss.getRange("'Today'!C3:Y20");
source.copyTo(destRange);
source.clear();
}
Note:
When you want to use getSheetByName, you can also modify your script as follows.
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Tmrw");
var source = srcSheet.getRange("C3:Y20");
var dstSheet = ss.getSheetByName("Today");
var destRange = dstSheet.getRange("C3:Y20");
source.copyTo(destRange);
source.clear();
}
References:
A1 notation
getRange(a1Notation) of Class Spreadsheet
copyTo(destination) of Class Range
getSheetByName(name)
I am not that good in script cause i begin, so i want to copy a sheet from a SpreadSheet (source) and copy it to my target SpreadSheet by creating a new sheet (in target SS) and name it with my value in cell C9 in my source sheet....
Actually the new sheet is create with the new name but nothing Copy in the target sheet. It seem to be my .copyTo(targetrange) who as the problem... I try a lot of think but nothing works.
can you help me!
function CopyToSpreadSheet() {
//Source sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Reception");
//Name new sheet in target SpreadSheet from value in cell C9
var nameSheet = sourceSheet.getRange(9, 3).getValue();
//open target SpreadSheet
var target = SpreadsheetApp.openById('SpreadSheet_Id');
var newSheet = target.insertSheet();
var Sheetname = newSheet.setName(nameSheet).getSheetName();
//CopyTo...
var targetSheet = target.getSheetByName(nameSheet);
var targetrange = targetSheet.getRange(1, 1, targetSheet.getLastRow()+1, targetSheet.getLastColumn()+1);
var sourceRange = sourceSheet.getRange(1, 1, 80, 15).copyTo(targetrange).getValues();
targetrange.setValues(sourceRange);
return;
}```
Solution
The copyTo method is available for Sheet Objects and for Range Objects. If you want to use it on a Range instance as you are doing at:
var sourceRange = sourceSheet.getRange(1, 1, 80, 15).copyTo(targetrange).getValues();
It has a limitation: you cannot copy a range to a different Spreadsheet.
Proposed modification
You can use the copyTo method of a Sheet instance in order to achieve the desired result:
function CopyToSpreadSheet() {
//Source sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Reception");
//Name new sheet in target SpreadSheet from value in cell C9
var nameSheet = sourceSheet.getRange(9, 3).getValue();
//open target SpreadSheet
var target = SpreadsheetApp.openById('target-sheet-id');
//CopyTo...
var targetSheet = sourceSheet.copyTo(target);
targetSheet.setName(nameSheet);
return;
}
Reference
Sheet copyTo
With this script ( Generate new spreadsheet from a selected row based on template ) I can generate a new spreadsheet (and so not a tab), based on a template tab (in this case, the tab "xxx") only when I select a specific row and rename this Spreadsheet as the value in the cell in column B for that corresponding row.
Now, I would copy the value of the cell A2 from the source spreadsheet into the tab "xxx" in the cell A3.
How to do that?
function onOpen() {
SpreadsheetApp.getUi().createMenu('Genera Scheda')
.addItem('Genera Scheda', 'createSpreadsheet')
.addToUi()}
function createSpreadsheet() {
var ss = SpreadsheetApp.getActive();
// the following line means that the function will search for the spreadsheet name in the active sheet, no matter which one it is
var sheet = ss.getActiveSheet();
//the selected row
var row = sheet.getActiveCell().getRow();
// column 2 corresponds to "B"
var name = sheet.getRange(row, 2).getValue();
var templateSheet1 = ss.getSheetByName('xxx');
var templateSheet2 = ss.getSheetByName('xxx2');
var newSpreadsheet = SpreadsheetApp.create(name);
var fileId = newSpreadsheet.getId();
var file = DriveApp.getFileById(fileId);
var folderId ="-----";
var folder = DriveApp.getFolderById(folderId);
templateSheet1.copyTo(newSpreadsheet).setName("Scheda");
templateSheet2.copyTo(newSpreadsheet).setName("Import")
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Foglio1"));
folder.addFile(file);
DriveApp.getRootFolder().removeFile(file);
}
To copy individual cell values between tabs and spreadsheets, a good solution is to use getValue() and setValue()
This methods must be applied to range objects (that is the cells).
Sample:
...
// define sheet either as the active sheet or specify it with sheet = ss.getSheetByName("XXX");
var myValue = sheet.getRange("A2").getValue();
newSpreadsheet.getSheetByName("xxx").getRange("A3").setValue(myValue);
...
Note that if you want to get / set values of more than one cell simulataneously, you need to use getValues() and setValues() instead.
UPDATE
Full code based on your situation:
function onOpen() {
SpreadsheetApp.getUi().createMenu('Genera Scheda')
.addItem('Genera Scheda', 'createSpreadsheet')
.addToUi()}
function createSpreadsheet() {
var ss = SpreadsheetApp.getActive();
// the following line means that the function will search for the spreadsheet name in the active sheet, no matter which one it is
var sheet = ss.getActiveSheet();
//the selected row
var row = sheet.getActiveCell().getRow();
// column 2 corresponds to "B"
var name = sheet.getRange(row, 2).getValue();
var templateSheet1 = ss.getSheetByName('xxx');
var templateSheet2 = ss.getSheetByName('xxx2');
var newSpreadsheet = SpreadsheetApp.create(name);
var fileId = newSpreadsheet.getId();
var file = DriveApp.getFileById(fileId);
var folderId ="-----";
var folder = DriveApp.getFolderById(folderId);
templateSheet1.copyTo(newSpreadsheet).setName("Scheda");
templateSheet2.copyTo(newSpreadsheet).setName("Import")
var myValue = sheet.getRange("A2").getValue();
newSpreadsheet.getSheetByName("Scheda").getRange("A3").setValue(myValue);
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Foglio1"));
folder.addFile(file);
DriveApp.getRootFolder().removeFile(file);
}
Script would run in the source workbook.
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1yeS6_qhURUYHVkImLNMjdGUeMWhPDwQKdLNgpygiwG4'); // sss = source spreadsheet
var ss = sss.getSheetByName('Sheet1'); // ss = source sheet
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('1KQBsMJ0vgBQwkM89d166RPyVahIZY3DRSOWVCB4zapE'); // tss = target spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName('Sheet1');
sheet.clear({ formatOnly: false, contentsOnly: true });
var tss = SpreadsheetApp.openById('1KQBsMJ0vgBQwkM89d166RPyVahIZY3DRSOWVCB4zapE');
var ts = tss.getSheetByName('Sheet1'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
Would set to trigger on form submit. Would copy the data from a sheet in one workbook to a sheet in another workbook. Format of both sheets will be same AND must remain unaltered. Row & column count/size will be same on each sheet.
Here a code that will go through all the sheet of your source workbook and copy data in the current workbook. Sheet name will be the same of source spreadsheet and only data are copied.
EDIT : Code changed to not copy but import data from spreadsheet to a dest spreadsheet where we assume that the sheet is already created. If sheet does not exist we create it.
function importWorkBook(){
var idSource = "IdOfTheSheetSource";
var sheets = SpreadsheetApp.openById(idSource).getSheets();
var current = SpreadsheetApp.getActiveSpreadsheet();
for(var i in sheets){
var sheet = sheets[i];
var data = sheet.getDataRange().getValues();
var destSheet = current.getSheetByName(sheet.getName());
if(destSheet){
destSheet.getRange(1,1,destSheet.getMaxRows(),destSheet.getMaxColumns()).clearContent();
destSheet.getRange(1,1,data.length,data[0].length).setValues(data);
Logger.log(sheet.getName() + " imported");
}else{
var newSheet = current.insertSheet(sheet.getName());
newSheet.getRange(1,1,data.length,data[0].length).setValues(data);
Logger.log(sheet.getName() + " created");
}
}
}
Stéphane