Why doesn't copyTo copy a value to a different sheet? - google-apps-script

Objective: Copy the contents of a cell on one sheet to another sheet.
Problem: Receive nothing instead of the cell contents.
Source code:
function DoesNotCopy() {
var sheet = SpreadsheetApp.getActiveSheet();
if (sheet.getSheetName() == 'GS') {
var sheetST = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
from = sheetST.getRange("B15:B15")
var to = sheet.getRange(13, 2, 1, 1);
from.copyTo(to);
}
}
The contents of cell B15 is:
=round(sum(C15:F15))
The contents of cells C15 to F15 is a 1 in cell C15.
The attachment DoesNotCopy1 shows the spreadsheet from which
the copy is made. The attachment DoesNotCopy2 show the spreadsheet to
which the copy is done.
How can I fix this problem?
Thanks,

function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const dsh = ss.getSheetByName('Sheet1');
sh.getRange("C20").copyTo(dsh.getRange("C20"), { contentsOnly: true });
}
Range.copyTo(destination , options)

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

How to overwrite the entire 'front' tab with values from the 'data' tab?

While there a few ways to iterate over a spreadsheet, here using sample code:
function onOpen(e) {
menuItems();
}
function menuItems() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('iterate', 'iterateThroughRows')
.addToUi();
}
function iterateThroughRows() {
var data = SpreadsheetApp.getActive().getSheetByName("data");
var front = SpreadsheetApp.getActive().getSheetByName("front sheet");
var values = data.getDataRange().getValues();
values.forEach(function (row) {
Logger.log(row);
SpreadsheetApp.getUi().alert(row);
//front.getRange().setValues(); //setRange? delete all data first?
});
}
So far as it goes, this works in that an alert pops up showing the data. Looking to overwrite the entire front page sheet with all data from the data sheet. How is it specified to write the values to the front sheet?
For the sake of simplicity, only concerned with data and not formulas or formatting.
To replace the contents of the 'front' sheet with the values in the 'data' sheet, use Sheet.clearContents(), Sheet.getDataRange(), Range.offset(), Range.getValues() and Range.setValues(), like this:
function replaceFrontSheetContentsWithDataSheetValues() {
const ss = SpreadsheetApp.getActive();
const frontSheet = ss.getSheetByName('front sheet');
const dataSheet = ss.getSheetByName('data');
const values = dataSheet.getDataRange().getValues();
frontSheet
.clearContents()
.getDataRange()
.offset(0, 0, values.length, values[0].length)
.setValues(values);
}
Copy all of the sheets of a spreadsheet to the first sheet
function copysstosh() {
const ss = SpreadsheetApp.getActive();
const tsh = ss.getSheets()[0];
tsh.clearContents();
tsh.appendRow([`Spreadsheet Copy: ${new Date()}`]);
ss.getSheets().filter((s,i) => i != 0).forEach(sh => {
let vs = sh.getDataRange().getValues();
tsh.getRange(tsh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
});
}

Add value offset 1 from active cell together in spreadsheet script?

I want to to add value of cell B1 onto the existing value of A1. I would like the script to be relative, and leave the A1 cell as a pure value. Bonus if cell B1 is cleared at the end of the script.
function myFunction() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var CurrentCell = cellRange.getValue ();
var RightCell = getvalue.offset(0, 1);
Logger.log(`selectedColumn: ${selectedColumn}`);
Logger.log(`selectedRow: ${selectedRow}`);
Logger.log(`selected cell vale: ${cellRange.getValue()}`);
Logger.log(`selected cell vale: ${cellRange.getValue(0, 1)}`);
cellRange.setValue(CurrentCell+RightCell);
It leaves with me with the value of A1 plus 'Range'. If A1 is 7 it returns (7Range).
Trying to do this in Google App Scripts
Add value of cell one column to the right to current cell.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r.getValue());
}
I get the same thing you do when I replace this with:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r);
}
And I'm kind of surprised it doesn't return an error. But yeah it returns the class name

create sheet with given name then copy values to the new sheet

I am stuck here. I would like to create a sheet with a name of a given cell in my main sheet (this part works). Then, I would like to copy the values from cells G4:I26 into the new sheet that was just created (not working). Here is what I have so far:
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getRange("J1").getDisplayValue();
ss.insertSheet(sheetName, ss.getSheets().length);
var source = ss.getRange('G4:I26');
source.copyTo(sheetName.getRange('A1'), {contentsOnly: true});
source.clear();
Try the following script code:
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var newSheetName = ss.getRange('J1').getDisplayValue();
var newSheet = ss.insertSheet(newSheetName, ss.getSheets().length);
var source = sheet.getRange('G4:I26');
source.copyTo(newSheet.getRange('A1'), {contentsOnly: true});
source.clearContent();
};
Change the Sheet name "Sheet1" in the above code with the sheet's name in which you have the cell "J1" containing the new sheet's name.

Google Spreadsheet opening the last edited cell using script

Using the following code allows the last edited cell to open when reopening a google spreadsheet.
function setTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction").forSpreadsheet(ss).onEdit().create();
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var sName = sheet.getName();
var currentCell = sheet.getActiveCell().getA1Notation();
UserProperties.setProperty("mySheetName", sName);
UserProperties.setProperty("myCell", currentCell);
}
function onOpen() {
var lastModifiedSheet = UserProperties.getProperty("mySheetName");
var lastModifiedCell = UserProperties.getProperty("myCell");
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(lastModifiedSheet).getRange(lastModifiedCell).activate();
}
I have a cell which has
=today()
Which updates so the code doesn't open the last row of my spreadsheet, but opens in the cell that updates with today().
How would I update the script to open the spreadsheet at my last edited cell in the last row of the spreadsheet?
Can you try this:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var sName = sheet.getName();
var currentCell = sheet.getActiveCell().getA1Notation();
var temp = "";
if (currentCell != "A1") {temp = currentCell;}
UserProperties.setProperty("mySheetName", sName);
UserProperties.setProperty("myCell", temp);
}
I added a temp variable and am doing a check (assuming only A1 has the today() function) before updating temp. Seems to work.