My Google App Script is Duplicating a whole sheet - google-apps-script

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);
}

Related

Google Apps Script to move multiple rows based on column value - not using OnEdit

I'm looking to edit this script so that before the rows are deleted, they are appended to the tab "Done". I tried creating a target sheet, but can't seem to get it working:
function remove(){
var SS = SpreadsheetApp.getActive();
var SHEET = SS.getSheetByName("Todo");
var TARGETSHEET = SS.getSheetByName("Done");
var RANGE = SHEET.getDataRange();
var DELETE_VAL = "Remove";
var COL_TO_SEARCH = 0;
var rangeVals = RANGE.getValues();
for(var i = rangeVals.length-1; i >= 0; i--){
if(rangeVals[i][COL_TO_SEARCH] === DELETE_VAL){
// TARGETSHEET.appendRow(i+1);
// TARGETSHEET.moveRows(rangeVals);
SHEET.deleteRow(i+1);
};
};
};
I essentially am trying to get it to operate like the script below, but for ANY value that has the word "Remove" in a specific column; not one by one. I want to run it from a button:
function onEdit(e){
var sourceSheet = e.range.getSheet();
if(sourceSheet.getSheetName() === 'Todo'){
var row = e.range.getRow();
var rowRange = sourceSheet.getRange(row, 1, 1, sourceSheet.getLastColumn());
var rowValues = rowRange.getValues()[0];
if(rowValues[0] === "Remove"){
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Done");
targetSheet.appendRow(rowValues);
sourceSheet.deleteRow(row);
}
}
}
Any thoughts are greatly appreciated
I believe your goal is as follows.
You want to check column "A" of "Todo" sheet, and when the value of column "A" is "Remove", you want to move the row to the destination sheet "Done".
You want to run the script by a button on the Spreadsheet.
In this case, how about the following sample script?
Sample script:
function myFunction() {
const srcSheetName = "Todo";
const dstSheetName = "Done";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(srcSheetName);
const dstSheet = ss.getSheetByName(dstSheetName);
const range = srcSheet.getDataRange();
let srcValues = range.getValues();
const dstValues = srcValues.filter(([a]) => a == "Remove");
srcValues = srcValues.filter(([a]) => a != "Remove");
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, dstValues.length, dstValues[0].length).setValues(dstValues);
range.clearContent().offset(0, 0, srcValues.length, srcValues[0].length).setValues(srcValues);
}
When this script is run, the above goal is obtained. In this sample, the sheet of "Todo" is updated by new values filtered by "Remove".
When you want to run this script by a button, please assign myFunction to the button.
Reference:
filter()

Google apps script / Spreadsheet app - copying values only

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);
};

How do i paste into another file through app script

I am using this code:
function copyInfo() {  
var ss = SpreadsheetApp.getActiveSpreadsheet();  
var copySheet = ss.getSheetByName("Product-analyse");  
var pasteSheet = ss.getSheetByName("Blad18"); // get source range  
var source = copySheet.getRange(93,7,39,6);  // get destination range  
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range  
source.copyTo(destination);  // clear source values
source.clearContent();
} 
Now its pasting in "Blad18". Instead I want it to be pasted in another file.
I tried adding {contentOnly: TRUE}. Though this didnt seem to work
I cannot figure this out unfortunately. Everything else is correctly set already.
Any help is greatly appreciated.
Updated script I am using:
function copyInfo() {
var ssSource = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ssSource.getSheetByName("Product-analyse");
var ssPaste = SpreadsheetApp.openById("1qhnA8L3ZkU-iEyOJtszAC4OW58sg2TJZSa_rmN_EXBI"); // the id of the other spreadsheet
var pasteSheet = ssPaste.getSheetByName("Submitted Data"); // get source range
var source = copySheet.getRange(93,7,39,6); // get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range
source.copyTo(destination); // clear source values
source.clearContent();
}
Try this:
function copyInfo() {
var ss = SpreadsheetApp.getActive();
var csh = ss.getSheetByName("Sheet0");
var psh = ss.getSheetByName("Sheet1");
const cshsr = 2;
var crg = csh.getRange(cshsr, 1, csh.getLastRow() - cshsr + 1, csh.getLastColumn());
var prg = psh.getRange(psh.getLastRow() + 1, 1);
crg.copyTo(prg);
}
I believe what you want to do is copy from one spreadsheet file to another spreadsheet file. Here is an example
function copyInfo() {
var ssSource = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ssSource.getSheetByName("Product-analyse");
var ssPaste = SpreadsheetApp.openById("dfsfhsfhskfhaskjfhsjkfas"); // the id of the other spreadsheet
var pasteSheet = ssPaste.getSheetByName("Blad18"); // get source range
var source = copySheet.getRange(93,7,39,6); // get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range
source.copyTo(destination); // clear source values
source.clearContent();
}
Reference
SpreadsheetApp.openById()

How do I make this Google Script faster?

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();

How to copy and paste as values Google Apps Script/Sheets from one spreadsheet to another?

So I have a huge spreadsheet with 9k rows and more than 30 columns. I want to copy this spreadsheet to another spreadsheet (Values only).
Previously I was using this code successfully, but due to the increase in data, the script now times out (1800s +). Is there a way to optimize this script or maybe an alternative option altogether?
function temp() {
var sss = SpreadsheetApp.openById('XYZ'); // sss = source spreadsheet
//var ss = sss.getSheets()[4]; // ss = source sheet
var ss = sss.getSheets(); // ss = source sheet
var id=4; //default number
for(var i in ss)
{
var sheet = ss[i];
if(sheet.getName()== "ABC")
{ id=i;
break;
}
}
console.log(id);
ss=sss.getSheets()[id];
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
SpreadsheetApp.flush();
var tss = SpreadsheetApp.getActiveSpreadsheet(); // tss = target spreadsheet
var ts = tss.getSheetByName('ABC'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
I believe your goal is as follows.
You want to reduce the process cost of the script.
In this case, I would like to propose to use Sheets API. This has already been mentioned in the Yuri Khristich's comment. Also, when the benchmark is measured between Spreadsheet service (SpreadsheetApp) and Sheets API, when Sheets API is used for reading and writing the values for Spreadsheet, it was confirmed that the process cost could be reduced. Ref
When Sheets API is used for your script, it becomes as follows.
Modified script:
Before you use this script, please enable Sheets API at Advanced Google services. And please set the source Spreadsheet ID and the sheet names.
function temp() {
var sourceSpreadsheetId = "XYZ"; // Please set the source Spreadsheet ID.
var destinationSpreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
var sourceValues = Sheets.Spreadsheets.Values.get(sourceSpreadsheetId, "ABC").values;
Sheets.Spreadsheets.Values.update({values: sourceValues}, destinationSpreadsheetId, "ABC", {valueInputOption: "USER_ENTERED"});
}
References:
Benchmark: Reading and Writing Spreadsheet using Google Apps Script
Method: spreadsheets.values.get
Method: spreadsheets.values.update
Copy from one spreadsheet to another
function copyfromonetoanother() {
const sss = SpreadsheetApp.getActive();
const dss = SpreadsheetApp.openById("dssid");
const ssh = sss.getSheetByName('Sheet1');
const vs = ssh.getDataRange().getValues();
const dsh = dss.getSheetByName('Sheet1');
dsh.getRange(dsh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
}
If you wish to select the source range:
function copyfromonetoanother() {
const sss = SpreadsheetApp.getActive();
const dss = SpreadsheetApp.openById("dssid");
const ssh = sss.getSheetByName('Sheet1');
const vs = ssh.activeRange().getValues();
const dsh = dss.getSheetByName('Sheet1');
dsh.getRange(dsh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
}
This function appends to the bottom of the destination sheet
function appenddatatobottomofdestination() {
const sssId = "source spreadsheet id";
const sss = SpreadsheetApp.openById(sssId);
const dss = SpreadsheetApp.getActive();
const dssId = dss.getId();
const ssh = sss.getSheetByName('Sheet1');//Source sheet
const srg = ssh.getRange(1,1,ssh.getLastRow(),ssh.getLastColumn());
const dsh = dss.getSheetByName("Sheet1");//Destination sheet
var vs = Sheets.Spreadsheets.Values.get(sssId, `${ssh.getName()}!${srg.getA1Notation()}`).values;
const drg = dsh.getRange(dsh.getLastRow() + 1, 1, vs.length,vs[0].length);//appends to bottom of spreadsheet
Sheets.Spreadsheets.Values.update({values: vs}, dssId, `${dsh.getName()}!${drg.getA1Notation()}`, {valueInputOption: "USER_ENTERED"});
}