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)
Related
I Have this code to copy all sheet data from one spreadsheet to another
function importData() {
var ss1 = SpreadsheetApp.openById("1_cujCKj2Xd-JT-bCu2-xSv2gij6qpQXzFtNQ6BY").getSheetByName("Sheet1").getDataRange();
var ss2 = SpreadsheetApp.openById("1N1tMU3ydG1poly4RDekdDGzlmsyRYLPn3LsfWmWg").getSheetByName("Sheet5");
var toRange = ss2.getRange(1, 1, ss1.getNumRows(), ss1.getNumColumns())
toRange.setValues(ss1.getValues());
}
But what i need to copy specific data range instead of copying all the sheet.
Can anyone help me so i can copy range like ("A1:E10") from the source spreadsheet to the cell ("B1") in the target sheet
In your situation, how about the following modification?
Modified script:
function importData() {
var srcRange = "Sheet1!A1:E10"; // Please set the source range as A1Notation.
var dstRange = "Sheet5!B1"; // Please set the destination range as A1Notation.
var ss1 = SpreadsheetApp.openById("1_cujCKj2Xd-JT-bCu2-xSv2gij6qpQXzFtNQ6BY").getRange(srcRange);
var ss2 = SpreadsheetApp.openById("1N1tMU3ydG1poly4RDekdDGzlmsyRYLPn3LsfWmWg").getRange(dstRange);
var srcValues = ss1.getValues();
ss2.offset(0, 0, srcValues.length, srcValues[0].length).setValues(srcValues);
}
When this script is run, the values are retrieved from "Sheet1!A1:E10", and the retrieved values are put to "Sheet5!B1".
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 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 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
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.