The script copies all cells in column V and pastes them one column to the right.
When this is done it should replace the cells with a formula in Column V with static values.
function newsprint() {
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 22, sheet.getMaxRows(), 1);
rangeToCopy.copyTo(sheet.getRange(1, 23));
}
You can do this with
function newsprint() {
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 22, sheet.getMaxRows(), 1);
rangeToCopy.copyTo(sheet.getRange(1, 23), {contentsOnly: true});
}
as shown here in the Google Apps Script Documentation. You need to use the copyTo(destination, options) method to use the contentsOnly parameter rather than
copyTo(destination).
Related
I'm trying to work out if it's possible to write a script that will copy and paste special all the values in a tab with the same name across multiple workbooks in a file?
This is what I have so far but I just can't seem to get it to work properly? Any help would be most appreciated.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Copy and Paste')
.addItem('Copy and paste sheet with new name', 'copyAndPasteSheetWithNewName')
.addToUi();
}
function copyAndPasteSheetWithNewName() {
var sheetName = Browser.inputBox('Enter the name of the sheet to copy and paste:');
var folder = DriveApp.getFileById(SpreadsheetApp.getActive().getId()).getParents().next();
var files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
var file = files.next();
var workbook = SpreadsheetApp.open(file);
var sheet = workbook.getSheetByName(sheetName);
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
}
From your following reply,
What I'm trying to do is just have the final part of the script hard code the worksheet. It's full of formulas and I want the results of those formulas not the formulas themselves.
In this case, how about the following modification?
From:
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
To:
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
range.copyTo(range, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false); // or range.copyTo(range, {contentsOnly: true});
By this modification, the last row of each sheet is converted to only the values without the formulas.
I want to copy the range a21:ay21 to a new sheet but the formula only copies the first value (a21).
I'm stuck with it. Thanks!
function submitToDataBase() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("form"); //Form Sheet
var datasheet = ss.getSheetByName("Data_Base"); //Data Sheet
//Input Values
var values = [[formSS.getRange("form!a21:ay21").getValues()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 50).setValues(values);
}
I think that in your script, values is a 4-dimensional array. And, a21:ay21 has 51 columns. If you want to use getValues and setValues, how about the following modification?
From:
var values = [[formSS.getRange("form!a21:ay21").getValues()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 50).setValues(values);
To:
var values = formSS.getRange("form!a21:ay21").getValues(); // formSS.getRange("a21:ay21").getValues();
datasheet.getRange(datasheet.getLastRow() + 1, 1, 1, 51).setValues(values);
As another method, I think that in your situation, you can use appendRow as follows.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("form"); //Form Sheet
var datasheet = ss.getSheetByName("Data_Base"); //Data Sheet
var values = formSS.getRange("form!a21:ay21").getValues(); // formSS.getRange("a21:ay21").getValues();
datasheet.appendRow(values[0]);
As another method, I think that in your situation, you can use copyTo of Class Range as follows.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("form"); //Form Sheet
var datasheet = ss.getSheetByName("Data_Base"); //Data Sheet
formSS.getRange("a21:ay21").copyTo(datasheet.getRange(datasheet.getLastRow() + 1, 1), { contentsOnly: true });
References:
getValues()
setValues(values)
appendRow(rowContents)
copyTo(destination, options)
Is this a named range?
var values = [[formSS.getRange("form!a21:ay21").getValues()]];
I would try deleting form from "form!a21:ay21", or simply using the named range writing the following in one of two ways
var values = [[formSS.getRange("form").getValues()]];
var values = [[formSS.getRange("!a21:ay21").getValues()]];
At the moment, I have to go through every sheet and use this macro to align it vertically and horizontally. Is there a way of tweaking this script to align all sheets instead of having to do each one manually?
function Alignment() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
spreadsheet.getActiveRangeList().setHorizontalAlignment('center')
.setVerticalAlignment('middle');
};
Get all sheets and iterate over each sheet:
function Alignment() {
var spreadsheet = SpreadsheetApp.getActive();
var sheets = spreadsheet.getSheets();
sheets.forEach(sheet=>{
sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).activate();
spreadsheet.getActiveRangeList().setHorizontalAlignment('center')
.setVerticalAlignment('middle');
});
};
If you want to get more performance but you won't be able to see the changes as they are hapenning in the sheets, then try this:
function Alignment() {
const spreadsheet = SpreadsheetApp.getActive();
const sheets = spreadsheet.getSheets();
sheets.forEach(sheet=>{
let rg=sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
rg.setHorizontalAlignment('center')
.setVerticalAlignment('middle');
});
};
My issue is, I have data being moved from one Sheet to another via script when marked "Complete". In the "Completed" sheet is where I want to sort the data. I already have an Autosort script that works only when debugging, not even on edit (tried editing everywhere), Let alone when data is moved to the sheet.
I want to be able to Auto sort whenever data is moved to the new sheet (I don't think its an onEdit function?)
I am not a programmer and I'm fairly new to Google Apps Script. I just dabble into code once in a while because I find it interesting, so I don't even know where to start to get the results I'm looking for.
This is the Auto Sort,
function AutoSortOnEdit() {
var sheetNames = ["Complete"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(function(name) {
var sheet = ss.getSheetByName(name);
var range = sheet.getRange(4, 1, sheet.getLastRow() - 1,
sheet.getLastColumn());
range.sort({column: 2, ascending: true});
});
}
EDIT: I realized I was using a script for multiple sheets. I changed it for one sheet, same issues though.
function AutoSortOnEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Completed Returns");
var range = sheet.getRange(4, 1, sheet.getLastRow() - 1,
sheet.getLastColumn());
range.sort({column: 2, ascending: true});
}
Any help would be greatly appreciated!
I managed to get it working by throwing the 2 scripts together under 1 function like so. I could not figure out how to get the event function to run while calling multiple functions, so I just combined them. Above the 2 "}" brackets is the move Script and below them is the Auto sort.
function onEdit(e) {
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
var actionCol = 24;
var nameCol = 24;
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
var colNumber = s.getLastColumn();
if (e.value == "TRUE" && colIndex == actionCol) {
var targetSheet = ss.getSheetByName("Completed Returns");
if (ss.getSheetByName("Completed Returns")) {
var targetSheet = ss.getSheetByName("Completed Returns");
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
sourceRange.copyTo(targetRange);
s.deleteRow(rowIndex);
}
}
var sheet = ss.getSheetByName("Completed Returns");
var range = sheet.getRange(4, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort({column: 2, ascending: true});
}
Thanks Everyone for the response!
You can add your function to one another
function AutoSortOnEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Completed Returns');
var range = sheet.getRange(
4,
1,
sheet.getLastRow() - 1,
sheet.getLastColumn()
);
range.sort({ column: 2, ascending: true });
}
function copyData() {}
function userActionRun() {
copyData();
AutoSortOnEdit();
}
At now you can bind it ti EDIT event
function onEdit(){
userActionRun();
}
You can use the Sort function to sort a data-set.
If you have a function that moves data form one sheet to another when the row is marked as complete, then you can add a line at the end of that to sort it.
You could also create a FilterView that sorts the whole sheet based on your parameters. Unlike Filters, FilterViews are saved between sessions.
Hello Everybody I tried to make scripts for copying filtered data but I did not make it.
I want to copy filtered data to another sheet automatically
function RejectSave() {
var ss = SpreadsheetApp.getActive();
var DataSheet = ss.getSheetByName('Import')
ss.setActiveSheet(DataSheet, true);
var sheet = ss.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).createFilter();
var criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(['', 'A', 'B', 'C'])
.build();
ss.getActiveSheet().getFilter().setColumnFilterCriteria(20, criteria);
var SaveSheet = ss.getSheetByName('RAW')
ss.setActiveSheet(SaveSheet, true);
ss.getActiveSheet().insertRowsAfter(ss.getActiveSheet().getMaxRows(), 5);
var SaveRange = SaveSheet.getRange(SaveSheet.getLastRow()+2, 1)
DataSheet.getRange(2, 1, DataSheet.getLastRow(), 26)
.copyTo(SaveRange, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
DataSheet.getFilter().remove();
};
but the script cannot bring just filtered data, it brings all data in that range, whether it meets the filter criteria or not.
I want to copy just filtered data!!
If the filter criteria is known before writing a script, then you probably should use JS array filter() method instead of Google sheet getFilter(). For example, the following function filters the source range A1:C10, excluding the rows with special values in column C. As a result it sets new (filtered) data array at start cell E1 (see also image).
function filtered() {
var sheet = SpreadsheetApp.getActiveSheet();
// Get full (non-filtered) data
var values = sheet.getRange('A1:C10').getValues();
// Apply filter criteria here
var hiddenValues = ['', 'A', 'B', 'C'];
values = values.filter(function(v) {
return hiddenValues.indexOf(v[2]) == -1;
});
// Set filtered data on the target sheet
sheet.getRange(1, 5, values.length, 3).setValues(values);
}
JS filtering is flexible and rather quick.