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);
}
Related
I have been trying to copy some data from a sheet to another but I have been running into some trouble.
I have to copy data and stop copying until the scripts finds an empty space, and I have to paste this data into another sheet where there's blank space (available space).
This is the code I have so far:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
var source = copySheet.getRange(11,1,1,10);
var destination = pasteSheet.getRange(1,1,1,10);
for (i = 1; i < 20; i++) {
if (destination.isBlank() == true) {
destination = pasteSheet.getRange(i, 1, 1, 10);
source = copySheet.getRange(i + 10, 1, 1, 10);
source.copyTo(destination);
} else {
destination = pasteSheet.getRange(i, 1, 1, 10);
}
}
}
It recognizes that the destination has an empty space, although it doesn't really paste it. The for (i = 1; i <20; i++) is for testing purposes.
If your data doesn't have any blank rows in between the first row and last non-empty row, then you can use this:
Sample Data:
sheet1
sheet2
Script:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
// get last rows of each sheet
var sLastRow = copySheet.getLastRow();
var dLastRow = pasteSheet.getLastRow();
// get data from sheet1 from row 11 to last row
var source = copySheet.getRange(11,1,sLastRow - 10,10).getValues();
// paste data to sheet2's last row
pasteSheet.getRange(dLastRow + 1,1,source.length,source[0].length).setValues(source);
}
Output:
Alternative:
If you have blank rows in between your sheet1, you can filter the values in sheet1.
Sample Data:
blank row 13 in sheet1
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
// get last rows of each sheet
var sLastRow = copySheet.getLastRow();
var dLastRow = pasteSheet.getLastRow();
// get data from sheet1 from row 11 to last row
// exclude rows with blank values in all columns
var source = copySheet.getRange(11,1,sLastRow - 10,10).getValues()
.filter(row => row.filter(col => !col).length < row.length);
// paste data to sheet2's first blank row
pasteSheet.getRange(dLastRow + 1,1,source.length,source[0].length).setValues(source);
}
Output:
I am using the below code to copy my data from sheet1 to sheet2 and append the data in sheet2 if script runs again? The issue is that my range is not fixed in sheet 1,I want to copy whatever range contains the content should be copied to sheet2 Can anyone guide that what to change in the below code to get the result?
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
var source = copySheet.getrange(2,1,200,7);
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,200,7);
source.copyTo(destination, {contentsOnly:true});
var data = copySheet.getRange(2,1,200,6)
data.clearContent()
}
Method1:
Include:
var paste_lr = pasteSheet.getLastRow()
Modify:
var source = copySheet.getrange(paste_lr+1,1,200,7);
Similarly change 200 using same function.
Method2:
var sp = PropertiesService.getScriptProperties();
var SourceLr = sp.getProperty("Source_Data_Rows") || 0;
var source = copySheet.getrange(SourceLr+1,1,200,7);
var Source_Lr = copySheet.getLastRow();
sp.setProperty("Source_Data_Rows", Source_Lr);
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);
}
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);
}
I am trying to copy a column range on on sheet and paste it in a row on a different sheet. I want it inserted to the top each time the script runs. This is what I got so far:
function Copy() {
var sss = SpreadsheetApp.openById('1UbcIcGJRVxsX9WbzunS96Ijf8c2gRc8UYb40lHpWWQU'); //source ID
var ss = sss.getSheetByName('Container Input'); //source Sheet tab name
var range = ss.getRange('B4:B23'); //assigned range to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('1UbcIcGJRVxsX9WbzunS96Ijf8c2gRc8UYb40lHpWWQU'); //destination ID
var ts = tss.getSheetByName('Container Log'); //destination sheet tab name
ts.getRange(1, 1, data.length, data[0].length).setValues(data); //you will need to define the size of the copied data see getRange()
//range.clearContent(); //clears var range
}
function Copy()
{
var sss = SpreadsheetApp.openById('1UbcIcGJRVxsX9WbzunS96Ijf8c2gRc8UYb40lHpWWQU');
//source ID
var ss = sss.getSheetByName('Container Input'); //source Sheet tab name
var ts = sss.getSheetByName('Cylinder Tracking'); //destination sheet tab name
//var protection = ts.protect();
//protection.setUnprotectedRanges(2,1,1,21);
ts.insertRowsBefore(2, 1);// Adds a new row before row 2 - Keeps the formatting of the
old row 2 for the new row 2
var source_C = 2;
var source_R = 3;
var dest_C = 2;
var dest_R = 2;
SpreadsheetApp.getActive().getRange('Cylinder Tracking!A2').setValue(new Date())
for (var counter = 1; counter <= 20; counter++)
{
var source_range = ss.getRange(source_R,source_C); //assigned range to copy
var data = source_range.getValues();
var dest_range = ss.getRange(dest_C,dest_R);
ts.getRange(dest_R, dest_C).setValues(data); //you will need to define the size of the
copied data see getRange()
source_R++;
dest_C++;
}
}