At first I had the error of my rows being out of bounds? Then I get this error with "TypeError: SpreadsheetApp.getActiveSpreadSheet is not a function" after I tampered a bit.
I'm not sure why my script isn't working. I have one function to just autoresize all my rows and another function to resize my rows a specific amount (this one keeps saying my rows are out of bounds) Both of these functions use to work for my rows, so I'm not sure what changed.
function onOpen() {
SpreadsheetApp.getUi().createMenu('Scripts')
.addItem('Hide columns', 'hideCols')
.addItem('Show Columns', 'show')
.addItem('Auto Resize Rows', 'autoResizeRows')
.addItem('Resize Rows', 'resizeRows')
.addToUi()
}
function hideCols() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.hideColumns(x.getColumn()));
}
function show() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.showColumns(x.getColumn()));
}
function autoResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadSheet();
var dataRange = ss.getDataRange();
var lastRow = dataRange.getLastRow();
ss.autoResizeRows(2, lastRow);
}
function resizeRows() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var cellss = sss.getSheetByName('Sheet1');
var responseData = cellss.getDataRange().getValues();
cellss.setRowHeights(2,responseData.length, 300);
}
Fixed:
function onOpen() {
SpreadsheetApp.getUi().createMenu('Scripts')
.addItem('Hide columns', 'hideCols')
.addItem('Show Columns', 'show')
.addItem('Auto Resize Rows', 'autoResizeRows')
.addItem('Resize Rows', 'resizeRows')
.addToUi()
}
function hideCols() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.hideColumns(x.getColumn()));
}
function show() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.showColumns(x.getColumn()));
}
function autoResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssname = ss.getActiveSheet()
var lastRow = ssname.getDataRange().getLastRow();
ssname.autoResizeRows(2, lastRow-2);
}
function resizeRows() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var cellss = sss.getSheetByName('Sheet1');
var responseData = cellss.getDataRange().getValues();
cellss.setRowHeights(2,responseData.length-2, 300);
}
Because you've a typo in this function:-
function autoResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadSheet();
var ssname = ss.getActiveSheet()
var lastRow = ssname.getLastRow();
ssname.autoResizeRows(1, lastRow);
}
It's getActiveSpreadsheet not getActiveSpreadSheet that's why your code is breaking, change that S to s.
Reference:-
getActiveSpreadsheet
Related
I got a function to shuffle multiple columns with different amount of values. For every column I have a function that shuffles the values in that column. I Combine all those functions into one function that shuffles all the columns.
function shuffleLevel345() {
shuffleColumnA();
shuffleColumnB();
shuffleColumnC();
shuffleColumnD();
shuffleColumnE();
}
function shuffleColumnA() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("A1:A101");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("B1:B99");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnC() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("C1:C89");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("D1:D124");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("E1:E75");
range.setValues(shuffleArray(range.getValues()));
}
Can I combine it to one function with all ranges?
I have tried it with:
var ranges = sheet.getRangeList(['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75']);
But that didn't work.
Probably it can be done with a loop this way:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
ranges.forEach(r => shuffleRange(r, sheet));
function shuffleRange(r, sheet) {
var range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
Or this way:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('level_345');
const ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
for (let r of ranges) {
let range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
Hello I am currently working on a time tracking system. With the following code I track the time how long a value was in a cell. This time is recorded in another worksheet and this is done continuously by appendRow ().
function onEdit(e) {
addTimestamp(e);
}
function addTimestamp(e) {
var ui = SpreadsheetApp.getUi();
var ws = "Tabellenblatt2";
var ss = e.source;
var targetSheet = ss.getSheetByName("Tabellenblatt1");
var range = targetSheet.getRange(3, 2, 1000, 1);
var currentDate = new Date();
var scriptProperties = PropertiesService.getScriptProperties();
if (e.source.getActiveSheet().getName() === ws) {
var cell = ss.getActiveCell();
var val = cell.getValue();
var sourceRowIndex = cell.getRow();
if (val != "") {
let rowToAdd = [val, "", currentDate, ""]
targetSheet.appendRow(rowToAdd);
scriptProperties.setProperty(sourceRowIndex, targetSheet.getLastRow());
} else {
var rowIndex = Number(scriptProperties.getProperty(sourceRowIndex));
if (rowIndex) targetSheet.getRange(rowIndex, 4).setValue(currentDate);
}
}
}
Now one Picture to show my Problem:
The problem is that the cells should start in row 1, is that possible with getLastRow ()?
Determine the last row with content based on another column (e.g. column A):
function onEdit(e) {
addTimestamp(e);
}
function addTimestamp(e) {
var ui = SpreadsheetApp.getUi();
var ws = "Tabellenblatt2";
var ss = e.source;
var targetSheet = ss.getSheetByName("Tabellenblatt1");
var range = targetSheet.getRange(3, 2, 1000, 1);
var currentDate = new Date();
var scriptProperties = PropertiesService.getScriptProperties();
if (e.source.getActiveSheet().getName() === ws) {
var cell = ss.getActiveCell();
var val = cell.getValue();
var sourceRowIndex = cell.getRow();
if (val != "") {
let rowToAdd = [val, "", currentDate, ""]
let rowA=targetSheet.getRange("A1:A").getValues().filter(String).length+1; // new code
targetSheet.getRange(rowA,1,1,rowToAdd.length).setValues([rowToAdd]); // new code
scriptProperties.setProperty(sourceRowIndex, targetSheet.getLastRow());
} else {
var rowIndex = Number(scriptProperties.getProperty(sourceRowIndex));
if (rowIndex) targetSheet.getRange(rowIndex, 4).setValue(currentDate);
}
}
}
it would be easier to change the formula for Time to not display anything at all unless there was an entry, then getLastRow() will work just fine. For help with that, share the formula that's calculating the time.
The error I am receiving:
Exception: The parameters (String,number) don't match the method signature for SpreadsheetApp.Spreadsheet.getRange.
function onOpen() {
var ui = SpreadsheetApp.getUi();
var VALUE = "Hide";
var COLUMN_NUMBER = 11;
ui.createMenu('Hidden Rows')
.addItem('Hide Rows w/Status = Hidden', 'menuItem1')
.addSeparator()
.addItem('Unhide Rows w/Status = Hidden', 'menuItem2')
.addToUi();
}
function menuItem1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var cell = ss.getActiveCell()
var cellValue = cell.getValue();
for (r=1; r<lastRow; r++) {
ss.getRange(r,11);
if(cellValue == VALUE){
activeSheet.hideRow(cell);
};
}
}
function menuItem2() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('Second menu item!');
}
Try this:
var VALUE = "Hide";//global
function menuItem1() {
var ss = SpreadsheetApp.getActive();
var activeSheet = ss.getActiveSheet();
var cell = ss.getActiveCell()
var cellValue = cell.getValue();
for (let r=1;r<lastRow;r++) {
if(cellValue==VALUE){
activeSheet.hideRow(cell.getRow());//this hides the row of the activeCell() not the row of the iteration index
}
}
}
These two values are not global scope in your script:
var VALUE = "Hide";
var COLUMN_NUMBER = 11;
They cannot get hoisted into global from function scope.
Interestingly enough that very line that caused the error does nothing.
ss.getRange(r,11);
You might have r defined to be a string some where so I declared r to have section scope in the loop. You should probably review const,var and let in a JavaScript reference.
It's not clear to me from your script what you had it mind but perhaps this is it.
function runOne() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const rg=sh.getDataRange();
const vs=rg.getValues();
const cell=ss.getActiveCell()
const cellValue=cell.getValue();
vs.forEach(function(r,i){
if(cellValue==r[10]){
sh.hideRows(i+1);
}
});
}
I am using code from this older thread to copy a range of cells from a source sheet and paste them to the last row of a destination sheet.
https://support.google.com/docs/forum/AAAABuH1jm04B0WxqiEJns/?hl=en&gpf=%23!topic%2Fdocs%2F4B0WxqiEJns
The suggested code copies and pastes the data on the same sheet so I attempted (and failed) to modify it:
function appendEagleData() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Eagle New Rows");
var dest = ss.getSheetByName("Eagle Static Data")
var source = sh.getRange("A4:J20");
var v = source.getValues();
dest.getRange(dest.getLastRow() + 1, 1, v.length, v[0].length).setValues(v);
}
...Clearly, I'm a little out of my depth. Any help would be appreciated.
function appendEagleData() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName("Eagle New Rows");
var rg1=sh1.getRange(4,1,sh1.getLastRow()-3,10);
var sh2=ss.getSheetByName("Eagle Static Data")
var rg2=sh2.getRange(sh2.getLastRow()+1,1);
rg1.copyTo(rg2);
}
try this:
function appendEagleData() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName("Eagle New Rows");
var rg1=sh1.getRange(4,1,getColumnHeight(2,sh1,ss)-3,10);
var sh2=ss.getSheetByName("Eagle Static Data")
var rg2=sh2.getRange(sh2.getLastRow()+1,1);
rg1.copyTo(rg2);
}
function getColumnHeight(col,sh,ss){
var ss=ss || SpreadsheetApp.getActive();
var sh=sh || ss.getActiveSheet();
var col=col || sh.getActiveCell().getColumn();
var rg=sh.getRange(1,col,sh.getLastRow(),1);
var vA=rg.getValues();
while(vA[vA.length-1][0].length==0){
vA.splice(vA.length-1,1);
}
return vA.length;
}
I have this Macro to send emails to some people. The problem is that it pastes the table in values, not in table format. I need to paste the table as a table.
function Mails() {
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var filas= values[0][1];
var columnas= values[1][0];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Enviar"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange(2,4,filas,columnas);
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = sheet.getRange(1, 3).getValue();
var destinatario = rowData[0];
var sigla1 = sheet.getRange(2,4,filas,columnas).getValues();
var mensaje = 'Estimado ' + destinatario + ',\n\n' + sigla1 ;
var asunto = 'Mensajes Pendientes';
MailApp.sendEmail(emailAddress, asunto, mensaje);
}
}
function onOpen () {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Emails', functionName: 'Mails'}
];
spreadsheet.addMenu('Enviar Emails', menuItems);
}
The problem is this line var sigla1 = sheet.getRange(2,4,filas,columnas).getValues();
I have to paste it as a table, not in values. How can i do that?
Thanks
I'am trying with this code now.. but it doesn't function..
function Mails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Enviar"));
var sheet = SpreadsheetApp.getActiveSheet();
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var filas= values[0][1];
var columnas= values[1][0];
var recipient = sheet.getRange(1, 3).getValue();
var subject = 'Estimado';
var vA=SpreadsheetApp.getActive().getSheetByName('Enviar').getDataRange().getValues();
var html="<style>th,td{border:1px solid black;}<table>";
for(var i=0;i<vA.length;i++) {
html+="<tr>";
for(var j=0;i<vA[i].length;j++) {
if(i==0) {
html+=Utilities.formatString('<th>%s</th>', vA[i][j]);
}else{
html+=Utilities.formatString('<td>%s</td>', vA[i][j]);
}
}
html+="</tr>";
}
GmailApp.sendEmail(recipient, subject, null, {htmlBody:html})
}
function onOpen () {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Emails', functionName: 'Mails'}
];
spreadsheet.addMenu('Enviar Emails', menuItems);
}
Here you have an image of what i need to send via email.
The rows change with the data, so the table depend of how many operations are in the day..
Html Table into Email
function sendHtmlTableViaEmail() {
var vA=SpreadsheetApp.getActive().getSheetByName('name').getDataRange().getValues();
var html="<style>th,td{border:1px solid black;}<table>";
for(var i=0;i<vA.length;i++) {
html+="<tr>";
for(var j=0;i<vA[i].length;j++) {
if(i==0) {
html+=Utilities.formatString('<th>%s</th>', vA[i][j]);
}else{
html+=Utilities.formatString('<td>%s</td>', vA[i][j]);
}
}
html+="</tr>";
}
//You have to add the code for recipient and subject
GmailApp.sendEmail(recipient, subject, null, {htmlBody:html})
}