function Backup() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('A'), true);
spreadsheet.getRange('A13:H').activate();
///Copy source to target and clear it
var target = SpreadsheetApp.openById("XXX");
var source_sheet = spreadsheet.getSheetByName("A");
var target_sheet = target.getSheetByName("B");
var source_range = source_sheet.getActiveRange();
var last_row = target_sheet.getLastRow();
var values = source_range.getValues();
target_sheet.getRange(last_row + 1, 1,values.length,values[0].length).setValues(values);
source_range.clearContent();
///How do I make this Google Script faster?
I believe your goal is as follows.
You want to reduce the process cost of your script.
In this case, how about the following patterns?
Pattern 1:
In this pattern, the Spreadsheet service (SpreadsheetApp) is used.
function sample1() {
var srcSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = srcSpreadsheet.getSheetByName("A");
var srcRange = srcSheet.getRange('A13:H' + srcSheet.getLastRow())
var srcValues = srcRange.getValues();
var dstSpreadsheet = SpreadsheetApp.openById("XXX"); // Please set the destination Spreadsheet ID here.
var dstSheet = dstSpreadsheet.getSheetByName("B");
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, srcValues.length, srcValues[0].length).setValues(srcValues);
srcRange.clearContent();
}
Pattern 2:
In this pattern, Sheets API is used. Before you use this script, please enable Sheets API at Advanced Google services.
function sample2() {
var srcSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = srcSpreadsheet.getSheetByName("A");
var srcRange = "'A'!A13:H" + srcSheet.getLastRow();
var values = Sheets.Spreadsheets.Values.get(srcSpreadsheet.getId(), srcRange).values;
var dstSpreadsheetId = "XXX"; // Please set the destination Spreadsheet ID here.
Sheets.Spreadsheets.Values.append({ values }, dstSpreadsheetId, "B", { valueInputOption: "USER_ENTERED" });
srcSpreadsheet.getRange(srcRange).clearContent();
}
References:
Method: spreadsheets.values.get
Method: spreadsheets.values.append
To speed up, don't use activate() in your script. Try
// source
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = spreadsheet.getSheetByName("A");
var source_range = source_sheet.getRange('A13:H');
var values = source_range.getValues();
// target
var target = SpreadsheetApp.openById("XXX");
var target_sheet = target.getSheetByName("B");
var last_row = target_sheet.getLastRow();
// copy
target_sheet.getRange(last_row + 1, 1, values.length, values[0].length).setValues(values);
source_range.clearContent();
Related
I want to copy / paste only the values from the source range, not the contained functions or formatting.
The option contentsOnly at the end of the script below is supposed to achive this, but actually it doesn´t. Does anyone see the problem?
Thanks so much!
function copyactiverowcontentsOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("1JwBik0vu3LTxCOTDpMUVSw5n6YxWz3BxZoLsx")
var source_sheet = ss.getSheetByName('not listed');
var target_sheet = target.getSheetByName('pairing');
var source_range = source_sheet.getActiveRange();
var target_range = target_sheet.getRange('A2:Q2');
source_range.copyTo(target_range),{contentsOnly:true};
};
Use that
source_range.copyTo(target_range, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
Try this:
function copyactiverowcontentsOnly() {
const ss = SpreadsheetApp.getActive();
const tss = SpreadsheetApp.openById("1JwBik0vu3LTxCOTDpMUVSw5n6YxWz3BxZoLsx")
const ssh = ss.getSheetByName('not listed');
const tsh = tss.getSheetByName('pairing');
const vs = ssh.getActiveRange().getDisplayValues();
const trg = tsh.getRange('A2:Q2');
tsh.getRange(2, 1, vs.length, vs[0].length).setValues(vs);
};
I am using the following app script to copy data from a certain range to another file. But for some reason this script is also copying and pasting the whole sheet. How to stop this?
function copyInfo() {
var destinationSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/189hJTiUlwubZdeN1HZ9n98U-gdaGdFSAu1-ARvlGMKo/edit#gid=0"; // Please set your Spreadsheet URL.
var destinationSheetName = "RawDataCopyPaste"; // Please set the destination sheet name.
// Source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Product-analyse");
var temp1 = copySheet.copyTo(ss);
var r = temp1.getDataRange();
r.copyTo(r, { contentsOnly: true });
// Destination sheet.
var dstSS = SpreadsheetApp.openByUrl(destinationSpreadsheetUrl);
var dstSheet = dstSS.getSheetByName(destinationSheetName);
var temp2 = temp1.copyTo(dstSS);
temp2.getRange(93, 6, 40, ).copyTo(dstSheet.getRange(dstSheet.getLastRow() + 2, 1));
// clear source values
copySheet.getRange(1, 2, 300, 9)
}
Example Source Sheet: https://docs.google.com/spreadsheets/d/1EKvIACMU7DMfuK9SQk8o6mmTTQ8_8a1qPt2jWy_q2MU/edit#gid=495001425
Example Dest Sheet: https://docs.google.com/spreadsheets/d/1mSDRsHPiPUlqg94x1Ocdssxr7fMJhqztch7kd_Xh_cE/edit#gid=0
Try it this way:
function copyInfoToAnotherFile() {
const sss = SpreadsheetApp.getActive();
const dss = SpreadsheetApp.openById("dssid");
const ssh = sss.getSheet("Sheet0");
const svs = ssh.getDataRange().getValues();
const dsh = dss.getSheetByName("Sheet0");
dsh.getRange(dsh.getLastRow() + 1, 1, svs.length,svs[0].length).setValues(svs);
}
I have the following code but I'm getting the following error after running it.
Is there any way to resolve this?
The data that I'm retrieving has about that amount of cell but it may be retrieving blank rows but all the rows has formulas.
N# of rows per file:
uno: 72850
dos: 72850
tres: 72850
cuatro: 72850
cinco: 72850
Total: 364250
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
The error message seems to be related to current Spreadsheets limits in Google Sheets as #Cooper mentioned, currently the limit is the following:
Up to 10 million cells or 18,278 columns (column ZZZ) for spreadsheets that are created in or converted to Google Sheets.
Creating a new sheet could do the work as the limits apply per sheets.
Try to replace getLastRow() by getLastDataRow() after adding
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastDataRow();
if (col == null){col='A'}
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
so ...
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastDataRow();
if (col == null){col='A'}
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
I am trying to copy and paste "A4:J4" and "A5:H5" but I am getting an error because A5 is merged. Also "STAGE 3" only pastes in "G4" in Sheet2. How can I copy and paste this data to Sheet2 with merged cells?
function hi()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var yourRange = sheet.getRange("A4");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = ss.getSheetByName('Sheet2');
var yourLastColumn=sheet.getRange("A4:4").getLastColumn();
var range = ss.getRangeByName("A4");
var yourLastRange=sheet.getRange("A4").offset(0, yourLastColumn-1);
var rsltRange1 = yourLastRange.getNextDataCell(SpreadsheetApp.Direction.PREVIOUS);
var rsltRange2 = sheet.getRange("A4:" + rsltRange1.getA1Notation());
if (rsltRange2.isPartOfMerge()) {
var rangeTest = rsltRange2.getMergedRanges()[0];
} else {
}
Logger.log(rangeTest.getA1Notation());
var destRange = destSheet.getRange("A4");
rsltRange2.copyTo(destRange, {contentsOnly: false});
}
Here is a simplified version of your code that should copy your range correctly no matter if the range is merged or not:
function hi(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var destSheet = ss.getSheetByName('Sheet2');
var startColumn=1;
var startRow=4;
var numRows=2;
var numColumns=sheet.getLastColumn()-startColumn+1;
var range=sheet.getRange(startRow, startColumn, numRows, numColumns);
var destRange = destSheet.getRange("A4");
range.copyTo(destRange);
}
I am trying to get a chart from spreadsheet A. I take this into a Chart object, and try to put it back in spreadsheet B.
I always get an 'unknown sheetname' error. I have been trying to put new ranges in the new chart, as you can see in the code. When I do a Logger.log(newrange.getSheet()) I indeed get Sheet instead of SHEETNAME, like the chart builder messes with the sheetnames.
What is happening?
function updateCharts() {
var ss = SpreadsheetApp.openById("ID");
var sheet = ss.getSheetByName("SHEETNAME");
var charts = sheet.getCharts();
var ss2 = SpreadsheetApp.openById("NEW ID");
var sheet2 = ss2.getSheetByName("SHEETNAME");
for (var c=0; c<charts.length; c++) {
var chart = charts[c];
var ranges = chart.getRanges();
var type = chart.getType();
var info = chart.getOptions();
var builder = chart.modify();
var range = ranges[0];
builder.removeRange(range);
var startRow = range.getRow();
var startCol = range.getColumn();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var newrange = ss2.getSheetByName("statistiek").getRange(startRow, startCol, numRows, numCols);
Logger.log(newrange.getSheet());
builder.addRange(newrange);
var title = chart.getOptions().get('title')
builder.setOption('title', title + " " + p);
var newchart = builder.build();
ss2.getSheetByName("statistiek").insertChart(newchart);
}
}
Logger.log() requires a string, but you're feeding it an object with newrange.getSheet(). Try this instead, if you want the sheet's name:
Logger.log(newrange.getSheet().getName())
To see the names of all sheets in a spreadsheet, try:
var ss = SpreadsheetApp.openById("ID");
var allSheets = ss.getSheets();
for (i in allSheets) {
Logger.log("Sheet " + i + " is named " + allSheets[i].getName());
}