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);
}
Related
I am trying to write a script to copy a range of cells to cell B2. The range to copy from changes each time so want to highlight the top left cell and then the script takes it from there. What I have so far is:
function CopyToB2() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var range = SpreadsheetApp.getActiveSheet().getRange(selectedColumn,selectedRow,2,43);
range.setValues(range);
spreadsheet.getRange('B2').activate();
(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
but get the error
Exception: The parameters (SpreadsheetApp.Range) don't match the
method signature for SpreadsheetApp.Range.setValues
Explanation:
The main issue with your code is that you pass a range object in the setValues function and therefore you are getting this error.
A second issue is that you passed the parameters of the getRange(row, column, numRows, numColumns) function in the wrong order. It should be:
getRange(selectedRow,selectedColumn,43,2)
instead of getRange(selectedColumn,selectedRow,2,43)
A third issue is that you need to define the range that the data will be pasted to and that can't be just a single cell B2 in this case. What you need to do is to define B2 as the start of your pasting range and then set the values of your active range.
Solution:
function CopyToB2() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var dataRange = sheet.getRange(selectedRow,selectedColumn,43,2).getValues();
sheet.getRange(2,2,dataRange.length,dataRange[0].length).setValues(dataRange);
};
I am working on a function where I copy a template sheet to another sheet.
Right now I have a demo where I hardcode the name of the template sheet that I want to add.
function AddFeature(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Defines Sheet that uses the function
var sheet = ss.getSheetByName("Client Balance");
//Get the template sheet
var templateSheet = ss.getSheetByName("Summary Sheet");
var totalrow = sheet.getLastRow();
for (var r = 2; r <= totalrow; r++){
var url = sheet.getRange(r,2).getValue();
var destSheet = SpreadsheetApp.openByUrl(url);
//Paste the template sheet to destination spreadsheet
templateSheet.copyTo(destSheet);
destSheet.getSheetByName("Copy of Summary Sheet").activate();
destSheet.moveActiveSheet(1); //Move the pasted template sheet to sheet at Index
destSheet.getSheetByName("Copy of Summary Sheet").setName("Summary Sheet"); //Rename the sheet
}
}
I want my script to draw the name of the sheet it needs to copy from a cell. I tried using this:
var fname = sheet.getRange(1,3).getValue;
var cname = sheet.getRange(1,4).getValue;
Where I would replace all "Summary Sheet" with : fname
and all "Copy of" with: cname
and they come from my sheet that looks like this:
Minimal reproductive example:
function AddFeature(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Defines Sheet that uses the function
var sheet = ss.getSheetByName("Client Balance");
//Defines which cells have the sheet name and "Copy of" sheet name
var fname = sheet.getRange(1,3).getValue;
var cname = sheet.getRange(1,4).getValue;
//Get the template sheet
var templateSheet = ss.getSheetByName(cname);
var totalrow = sheet.getLastRow();
for (var r = 2; r <= totalrow; r++){
var url = sheet.getRange(r,2).getValue();
var destSheet = SpreadsheetApp.openByUrl(url);
//Paste the template sheet to destination spreadsheet
templateSheet.copyTo(destSheet);
destSheet.getSheetByName(cname).activate();
destSheet.moveActiveSheet(1); //Move the pasted template sheet to sheet at Index
destSheet.getSheetByName(cname).setName(fname); //Rename the sheet
}
}
The statements that get the sheet names try to fetch the values from cells C1 and D1. Your screenshot suggests that the values should instead be fetched from cells C2 and D2. Try this:
var fname = sheet.getRange('C2').getValue();
var cname = sheet.getRange('D2').getValue();
I'm trying to make a script that will copy a cursor selection range to an other sheet, script is partially working
The script is coping some values, some of the values selected from the target sheet
function copy(){
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveRange();
var srange = range.getA1Notation();
var crange= ss.getRange(srange);
var data = crange.getValues();
var tss = SpreadsheetApp.openById('id');
var ts = tss.getSheetByName('Tracker');
ts.getRange('B16:K200').setValue(data)
}
The script should copy the selected value with the mouse from the selected sheet in the specified location for the target sheet
The key is to identify the activerange, and the relevant get row and column values. Then using "B16" as a starting point for the outout, you simply add the number of Rows and number of Columns calculated from the activerange.
Highlight any range, run the function, and the contents of the range will be copied to the same sheet with cell "B16" in the top lefthand corner.
It's a simple matter to modify the code to create the output range and copy the values to another spreadsheet.
function so5666559701() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "56665597";
var sheet = ss.getSheetByName(sheetname);
var myrange = sheet.getActiveRange();
var myvalues = myrange.getValues();
//Logger.log("DEBUG: the active range = "+myrange.getA1Notation());
var myfirstRow = myrange.getRow();
var mylastRow = myrange.getLastRow();
var myfirstCol = myrange.getColumn();
var mylastCol = myrange.getLastColumn();
//Logger.log("DEBUG: details of the selectred range: row: first row="+myfirstRow+", first column="+myfirstCol+", last row="+mylastRow+" last column:"+mylastCol);
// target cell = B16 = row 16, column 2
var targetrange = sheet.getRange(16,2,mylastRow-myfirstRow+1,mylastCol-myfirstCol+1);
//Logger.log("DEBUG: the target range = "+targetrange.getA1Notation());
targetrange.setValues(myvalues);
}
In an effort to organize a set of bank account and credit card statements, I download them. Then I open them in Excel or Google Sheets, reorganize them in matching columns, put all in one tab (TOTAL), and then sort.
The question is now how I do the copy from the individual sheets. I think I'm pretty close after pasting and repeating (the manual way) some function I found by searching. The problem is that this function removes all formatting from the source rows
Example sheet
The last tab example_TOTAL is put there only to show how I want tab TOTAL to look after the process is done.
Here's my function so far which does the most, but erases formatting (colors).
function Copy() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
var ts = spreadsheet.getSheetByName('TOTAL'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabB'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabC'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabD'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function copyFromTo() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA');
var copy = ss.getRange('A1:L');
var target = spreadsheet.getSheetByName('TOTAL');
var dest = target.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabB');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabC');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabD');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
}
How can I append from these sheets and still keep the formatting?
I am working on a script to automatically add a new row after column C on a daily basis. Below is my script:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("test");
var source = sheet.getRange("c4:U4");
var values = source.getValues();
sheet.insertRowBefore(4);
sheet.getRange("c5:U5").setValues(values);
};
Column A in the "test" sheet is a range imported from another sheet. The code above adds a new row then copies the range C-U to row 5.
But I what I want is to add a row from column C only.
however coz the requirement change i just copy over the the whole line with format instead of value
function addFirstRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var firstRow = 3;
var sh = ss.getSheetByName("daily");
var lCol = sh.getLastColumn();
var range = sh.getRange("A4:U4");
var formulas = range.getFormulas();
sh.insertRowsAfter(3,1);
newRange = sh.getRange("A4:U4");
newRange.setFormulas(formulas);
}