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');
});
};
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 have a form that is filled in and i would like for the answers coming in to be formatted as follow:
Text to be Wrapped, Centred Vertically as well as horizontally within the cell. As well as for the answers to be sorted from the youngest to the oldest based on column A
I have managed to pull some scripts but cannot seem to get them working in conjunction. Please help !!
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s= ss.getActiveSheet()
var lr = s.getLastRow()
var r= s.getRange(1, 1, lr,4)
var set=r.setHorizontalAlignment("center")
var range = e.range;
range.setWrap(true);
}
function sortResponses() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Form Responses 1");
sheet.sort(1, false);
}
and second
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');
});
};
Please help
Here is a little script snippet I use for doing a similar thing.. Its not my best code but does the job.
The following function is set to run with a On Form Submit trigger,
function sort_and_format_FormResponses() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
//Sort Responses by Date Stamp
var range = sheet.getRange("A2:G");
range.sort({column: 1, ascending: false})
range.setWrap(true);
//Column Specific Formatting...
//Format column D to Plain text
range = sheet.getRange("D:D");
range.setNumberFormat('#STRING#');
range.setHorizontalAlignment('left');
}
function sort_and_format_FormResponses() {
var sh = SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
var rg = sh.getRange("A2:G" + sh.getLastRow());//Don't use unterminated ranges like A2:G in apps script because in generates a lot of nulls for lastrow to maxrows
rg.sort({column: 1, ascending: false})
rg.setWrap(true);
rg = sh.getRange("D1:D" + sh.getLastRow());
rg.setNumberFormat('#STRING#');
rg.setHorizontalAlignment('left');
}
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)
I really need your help. After 2 days, I'm still on "ZERO".
I need a script that copies all the formats from an active sheet to every other sheet.
It would be nice to have the conditional formatting, alternating colors and data validation too.
I think it's not so hard, but I'm really new in script writing.
This is the sheet:
https://docs.google.com/spreadsheets/d/1mOkm_r4rngPXTkIerSQJKxRih31sM7XoZCZVnoHUc5M/edit?usp=sharing
The code so far:
enter function copyFormatting(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var copyTo = ss.getSheetByName('agent2');
var range = copyTo.getRange(1, 1, copyTo.getMaxColumns(), copyTo.getMaxRows());
sheet.getBandings()[0].copyTo(copyTo.getRange(1,1)),SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
}
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
copyFormatting(); // the function what yo like to run
}
})
}
I was able to figure out and write some functions: clear formatting on all sheets, format row headers on all sheets, show a list of sheets, but this one is too hard for me.
Even if I try to model it first with the macro recorder.
I would be very grateful, if you could help me with this issue.
I was able to figure it out:
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
var sampleSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var maxRow =sampleSheet.getMaxRows();
var maxCol =sampleSheet.getMaxColumns();
var sampleRange = sampleSheet.getRange(1,1,maxRow,maxCol);
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
var targetSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var targetRange = targetSheet.getRange(1,1);
sampleRange.copyTo(targetRange, {formatOnly:true}) // it copies the formatting
}
})
}
Reference
Copy To
Try insertSheet(options) and if that doesn't work the try copyTo(destination, options)
I want to remove the filtration from the spreadsheet using a script and after i remove filtered rows the sheet display all row in sheet
I want to do that when I open the spreardsheet.
function ShowColumns() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var maxRows = sheet.getMaxRows();
var maxCol = sheet.getMaxColumns();
sheet.showRows(1, maxRows);
sheet.showColumns(1,maxCol)
}
I searched for it intensively, but did't find anything.
There is a way to do it with Google advanced service - Google Sheets API. And here is my function to turn off the filter in Google Sheet.
function removeFilter(sheet){
var ss = SpreadsheetApp.getActive();
var spreadsheetId = ss.getId();
var sheetId = sheet.getSheetId();
var resource = {
"requests": [
{
"clearBasicFilter": {
"sheetId": sheetId
}
}
]
}
Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
}