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);
}
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 copy the data of Sheet1 to Sheet2 with the code below.
When I inserted an arrayformula(A2:A&B2:B) in Sheet2, the sript stopped working and i got the error:
coordinates of the target range are outside the dimensions of the
sheet array formula
When I deleted the arrayformula, the execution of the script completed successfully.
What should i change to the code (or to the arrayformula) to get them to cooperate?
function dailyLog() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var logSheet = ss.getSheetByName('Sheet2');
var lastRow = logSheet.getLastRow();
var range = sourceSheet.getDataRange();
range.copyTo(logSheet.getRange(lastRow + 2, 1), {contentsOnly: true}); // paste under previous paste
}
Issue:
The input range and the output range do not match:
Input range: range = sourceSheet.getDataRange()
Output range: logSheet.getRange(lastRow + 2, 1)
The output range is a single cell, while the input range is the full data range (therefore multiple cells).
Solution:
If the input range is dynamic it is a better idea to use getValues/setValues instead:
function dailyLog() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var logSheet = ss.getSheetByName('Sheet2');
var lastRow = logSheet.getLastRow();
var values = sourceSheet.getDataRange().getValues();
logSheet.getRange(lastRow+1,1,values.length,values[0].length).setValues(values);
}
and now the size of the data (values) is dynamically set with the help of the length function.
Reference:
getValues()
setValues()
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);
}
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);
}