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);
}
Related
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);
Here is a script I'm using that works, but is trying to import too much data. I apologize for my ignorance when it comes to this.
function copyPaste() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("Print Results");
var rangeSource = source.getDataRange();
var data = rangeSource.getValues();
var lr = rangeSource.getLastRow();
var lc = rangeSource.getLastColumn();
Logger.log(data);
var sss = SpreadsheetApp.openById("XXXXXX");
var target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,lr,lc).setValues(data);
}
What I need this to do is only select A8:M, and only copy over the rows that have a USER ID in column J. The USER ID is always a number, so a simple rule of ">0" should work for that. USER ID is imported from a different tab using a vlookup formula so the cell is not "empty".
Only copy rows where Column J is not null
function copyPaste() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Print Results");
const rg = sh.getDataRange();
const vs = rg.getValues().filter(r => r[9] != '');
Logger.log(vs);
const sss = SpreadsheetApp.openById("XXXXXX");
const target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,vs.length,vs[0].length).setValues(vs);
}
.filter() method
having trouble with the following code:
function addDelivery() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Sheet7");
var range = srcSheet.getRange("B3:L3").getValues();
var formulas = srcSheet.getRange("B3:L3").getFormulas();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,1,11).setValues(range);
range.clearContents();
range.setFormulas(formulas);
I want to clear the contents of cells B3:L3 but keep the formula in L3.
I get the error message "TypeError: range.clearContent is not a function".
I read that clearContent is a method of Range and clearContents is a method of sheet.
I've tried both, but neither work.
Please help.
Best regards
manc
Perhaps this would work better:
function addDelivery() {
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getSheetByName("Sheet7");
var range=srcSheet.getRange("B3:L3");
var values = range.getValues();
var formulas = range.getFormulas();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,1,11).setValues(values);
range.setFormulas(formulas);
srcSheet.getRange("B3:K3").clearContent();
I thought about it some more and I think I'd probably do it this way:
function addDelivery() {
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getSheetByName("Sheet7");
var range = srcSheet.getRange("B3:L3");
var values = range.getValues();
var formula = srcSheet.getRange("L3").getFormula();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,values.length,values[0].length).setValues(values);
srcSheet.getRange("L3").setFormula(formula);
srcSheet.getRange("B3:K3").clearContent();
i'm really newbie at scripting, i used to do my work with microsoft office that have a special paste "add" feature and now for google sheet i can't really find it.
I will have a source range of C2:C102 and destination at same sheet D2:D102 i want the script (that i can run manually weekly) to copy all the range from source and sum it with the already existing data at D2:D102 (only values).
Here is a small example - Before after
I tried to use this code but ofc it just replaces the values.
function copyCells(){
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var SourceSheet = thisSpreadsheet.getSheetByName("test");
var SourceRange = thisSpreadsheet.getRange("C2:C102");
var destinationSheet = thisSpreadsheet.getSheetByName("test");
var destinationRange = destinationSheet.getRange("D2:D102");
SourceRange.copyTo(destinationRange, {contentsOnly: true});
}
Any help will be really appreciated :)
Haven't tested the code but try this.
grab values with getValues()
sum the values
copy back values with setValues()
function copyCells(){
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const SourceSheet = spreadsheet.getSheetByName("test");
const SourceRange = spreadsheet.getRange("C2:C102");
const SourceValues = SourceRange.getValues();
const destinationSheet = spreadsheet.getSheetByName("test");
const destinationRange = destinationSheet.getRange("D2:D102");
const destinationValues = destinationRange.getValues();
for (let i = 0; i < SourceValues.length; i++)
destinationValues[i][0] = parseFloat(destinationValues[i][0]) + parseFloat(SourceValues[i][0])
destinationRange.setValues(destinationValues);
}
REFERENCES
range.getValues()
range.setValues()
I am looking for a little bit help on this situation. I have a spreadsheet where I create a new sheet, I rename it, and once this is done I do the same for like around 10 other spreadsheets, I would like to know how to implement a for or a while loop so that I dont have to type the repeated code 10 times. My code is below, any help would be really appreciate it. I have the keys in another document, so basically I need to know how to be able to read the keys in a variable and then be able to read each key since they are in a vertical column.
function create(){
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
ss0.setActiveSheet(ss0.getSheetByName("old"));
ss0.setActiveSheet(ss0.duplicateActiveSheet());
ss0.renameActiveSheet("main");
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
ss0.getRange("A4:I500").clearContent();
//repeated code key 1
var ss0 = SpreadsheetApp.getActiveSheet();
var master = SpreadsheetApp.openById("key1");
ss0.copyTo(master);
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
master.setActiveSheet(master.getSheetByName("Copy of main"));
master.renameActiveSheet("main");
// key 2
var ss0 = SpreadsheetApp.getActiveSheet();
var master = SpreadsheetApp.openById("key2");
ss0.copyTo(master);
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
master.setActiveSheet(master.getSheetByName("Copy of main"));
master.renameActiveSheet("main");
}
You need to get the keys in an array and loop over this array.
Here is an outline
function myFunction() {
//get the keys in an array
//say, they are in column A in a spreadsheet from A1:A10
var keyArray = SpreadsheetApp.openById("ID of the ss with key").getSheetByName("name of the sheet with key")
.getRange("A1:A10").getValues();
var ss0 = SpreadsheetApp.getActiveSheet();
//loop for all the spreadsheets
for(var i in keyArray){
var ss = SpreadsheetApp.openById(keyArray[i][0]);
ss0.copyTo(ss);
ss.getSheetByName("Copy of main").setName("main")
}
}