Google apps script / Spreadsheet app - copying values only - google-apps-script

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

Related

copyTo(target) copies my functions but need it to copy values

So this might be a simple one for someone more skilled. The code works except it copies the functions rather than the values. The cells contain a LOT of functions and I'm using this to create a second version since the functions are UI data reliant and this is the easiest way to make a "Backup" in case we need to go back to an old forms data. I'm using the below code and it copies everything over, and creates the sheet with the right name, but I'm not sure how to get it to take the values and not the functions.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target);
targetSheet.setName(nameSheet);
I've tried this,
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target,SpreadsheetApp.CopyPasteType.PASTE_VALUES);
targetSheet.setName(nameSheet);
but it gives me the following error:
Exception: The parameters (SpreadsheetApp.Spreadsheet,SpreadsheetApp.CopyPasteType) don't match the method signature for SpreadsheetApp.Sheet.copyTo.
Any help or advice on another script I could use would be appreciated. Thanks.
Try this:
function copyvalues() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("AHA");
const vs = sh.getDataRange().getValues();
const name = sh.getRange(1, 15).getValue();
const tss = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
const tsh = tss.insertSheet(name);
tsh.getRange(1,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()

My Google App Script is Duplicating a whole sheet

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

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

Copy filter data from one spreadsheet file to another - Google Apps Script

I'm currently working on a project where I need to copy data to another google sheet.
let me explain:
I'd created a function where I applied filter by text and it works. But the issue is that I can't copy only the filtered data to another sheet. I'd seen many stack conversations but nothing fit.
here is the code
function copyWithValues() {
const spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = spreadSheet.getSheetByName('testo');
const sourceRange = sourceSheet.getDataRange();
const sourceValues = sourceRange.getValues();
const targetSpreadsheet = titleAsDate();
let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;
let targetSheet = targetSpreadsheet.getSheetByName('Sheet1').setName("Report");
let targetRange = targetSheet.getRange(1, 1, rowCount, columnCount);
targetRange.setValues(sourceValues);
}
Explanation:
The following solution is inspired by this answer.
The latter has one limitation though, it only works for transferring the filter data between sheets of the same spreadsheet.
We are going to use the exact same answer to copy the filter values from the source sheet and paste them to a temporary sheet temp_sheet of the source spreadsheet file.
Then we simply get the values of that temporary file and paste them to another spreadsheet file with the regular method of getting and setting the values as you have already implemented in your code.
Solution:
function copyWithValues() {
const spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = spreadSheet.getSheetByName('testo');
const temp_sheet = spreadSheet.insertSheet('temp_sheet');
const sourceRange = sourceSheet.getFilter().getRange();
sourceRange.copyTo(
temp_sheet.getRange('A1'),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
false);
SpreadsheetApp.flush();
const sourceValues = temp_sheet.getDataRange().getValues();
const targetSpreadsheet = titleAsDate();
const rowCount = sourceValues.length;
const columnCount = sourceValues[0].length;
const targetSheet = targetSpreadsheet.getSheetByName('Sheet1').setName("Report");
const targetRange = targetSheet.getRange(1, 1, rowCount, columnCount);
targetRange.setValues(sourceValues);
spreadSheet.deleteSheet(temp_sheet);
}
where the helper function titleAsDate() is defined as:
function titleAsDate() {
const currentDate = Utilities.formatDate(new Date(), "GMT+8", "dd-MM-yyyy HH:mm:ss");
return SpreadsheetApp.create("Report of the " + currentDate);
}
based on your previous question.