Change Row Alignment Based on Cell Value - Google Scripts - javascript

the following script is supposed to change the row alignment in a Google sheet to 'right' if the value of column E in the row is 'Post.' When I run the script it does not pop any errors, however, it does not do anything either.
Can anyone take a look at the below and provide some guidance?
Thanks!
function rowLoop() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("KOD Tests");
var endRow = ss.getLastRow();
for (var r = 1; r < endRow; r++) {
rowAlignment(r);
}
}
function rowAlignment(r) {
var sheet = SpreadsheetApp.getActiveSheet();
var c = sheet.getLastColumn();
var dataRange = sheet.getRange(r, 1, 1, c);
var data = dataRange.getValue();
var row = data[4];
if(row === 'Post') {
data.setHorizontalAlignment('right');
}
SpreadsheetApp.flush();
}

Here it is with a few things fixed (please see comments)
function rowLoop() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var endRow = ss.getLastRow();
// <= to repeat for all rows
for (var r = 1; r <= endRow; r++) {
rowAlignment(r);
}
}
function rowAlignment(r) {
var sheet = SpreadsheetApp.getActiveSheet();
var c = sheet.getLastColumn();
var row = sheet.getRange(r, 1, 1, c);
// Get cell in column E of row
var cell=row.getCell(1,5);
// Get its value
var data = cell.getValue();
// Test equal to 'Post' with ==
if(data == 'Post') {
row.setHorizontalAlignment('right');
}
SpreadsheetApp.flush();
}

Related

Deleting row in one sheet if cell value is found in another sheet

I am trying to delete rows if cell value is found in another sheet via App Script. But the code is not going through complete data in history sheet.
function deleteOld1() {
var app = SpreadsheetApp;
var orderSheet = app.getActiveSpreadsheet().getSheetByName("RC"); \sheet where to look
var historySheet = app.getActiveSpreadsheet().getSheetByName("OB"); \sheet from which data has to be deleted
var lastRow = historySheet.getLastRow();
var lastRow1 = orderSheet.getLastRow();
var poNO = orderSheet.getRange("A2:A" + lastRow);
var myRange = historySheet.getRange("A2:A" + lastRow1);
var row = poNO.getRow();
var col = poNO.getColumn();
var data = myRange.getValues();
var data1 = poNO.getValues();
if (col>= poNO.getColumn() && col <= poNO.getColumn() && row>= poNO.getRow() && row <= poNO.getRow()){
for (i=0; i<data.length; i++) {
for (j=0; j<data1.length; j++){
if(data[i][0] == data1[j][0]){
historySheet.deleteRow(i+2);
}
}
}
}
}
Please go through this code and help

Setting a value in a drop down with Google Sheets using a script

I need to figure out a way to set a drop down value based on if another cell in the same row has a value. So for instance if D2 has a value other than '' or null; H2 needs to be set to "New Issue". The answers found in another similar post don't actually work with this since I am using formRedirector. Things got really weird last time. I tried to delete the earlier post but couldn't.
I have some of the logic figured out, but the issue is that what I have now writes "New issue" to H3 and below. Here is what I have so far:
var NEW_ISSUE = 'New Issue';
function defaultValue() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 900;
var dataRange = sheet.getRange(startRow, 4, numRows);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var default_status = row[7];
if (default_status != NEW_ISSUE && row == '') {
sheet.getRange(startRow + i, 8).setValue(NEW_ISSUE);
}
}
}
I have a feeling somewhere in the if statement I am messing up somewhere.
This solved the issue, thank you
var NEW_ISSUE ='New Issue';
var row;
var default_status;
function defaultValue() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1;
var numRows = 900;
var dataRange = sheet.getRange(startRow, 4, numRows);
var data = dataRange.getValues();
var statusRange = sheet.getRange(startRow,8,numRows);
var status = statusRange.getValues();
for (var i = 0; data[i] != ''; ++i)
{
if (status[i] == '')
{
sheet.getRange(i+startRow, 8).setValue(NEW_ISSUE);
}
}
}

Delete row in Google Sheets that contains any number of defined keywords

I would like to delete rows in Google Sheets that contain any keyword that is a member of some pre-defined set. I currently have the following code:
function removeKeywords() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0].indexOf("admin","conference","department") > -1) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
Where the keywords 'admin', 'conference', and 'department' trigger the function to delete the row. Yet my code doesn't work. Help would be much appreciated.
See if this works
function removeKeywords() {
var sheet = SpreadsheetApp.getActiveSheet();
var rowsDeleted = 0;
sheet.getDataRange()
.getValues()
.forEach(function (r, i) {
if (["admin", "conference", "department"].indexOf(r[0]) > -1) {
sheet.deleteRow((parseInt(i) + 1) - rowsDeleted);
rowsDeleted++;
}
})
}

Add value (n+1) in last empty row

I have a problem, I don't know how create button in a Google spreadsheet which will find the last empty row and add in first column next number (n+1) and in second column actual date.
function selectFirstEmptyRow() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.setActiveSelection(sheet.getRange("A" + getFirstEmptyRowWholeRow()))
// copy value from row
var ss = SpreadsheetApp.getActiveSheet();
ss.insertRowAfter(ss.getLastRow());
ss.getRange(1, 1).copyTo(ss.getRange(ss.getLastRow() + 1, 1));
// add 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeRange = ss.getActiveRange();
var cell, cellValue, cellFormula;
// iterate through all cells in the active range
for (var cellRow = 1; cellRow <= activeRange.getHeight(); cellRow++) {
for (var cellColumn = 1; cellColumn <= activeRange.getWidth(); cellColumn++) {
cell = activeRange.getCell(cellRow, cellColumn);
cellFormula = cell.getFormula();
if (cellFormula[0] != "=") {
cellValue = cell.getValue();
cell.setValue(cellValue + 1);
}
}
}
}
function getFirstEmptyRowWholeRow() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var row = 3;
for (var row = 3; row < values.length; row++) {
if (!values[row].join("")) break;
}
return (row + 1);
}
You can add a custom menu to a Google spreadsheet:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('My menu')
.addItem('Add new date row', 'insertAtNextEmptyRow')
.addToUi();
}
function insertAtNextEmptyRow() {
var firstRow = 3;
var sheet = SpreadsheetApp.getActiveSheet();
var row = getNextEmptyRow(firstRow);
var previousNum = SpreadsheetApp.getActiveSheet().getRange('A' + (row - 1)).getValue()
if (previousNum !== parseInt(previousNum, 10))
previousNum = row - firstRow - 1;
SpreadsheetApp.getActiveSheet().getRange('A' + row).setValue(previousNum + 1);
var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
SpreadsheetApp.getActiveSheet().getRange('B' + row).setValue(date);
}
function getNextEmptyRow(firstRow) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for (var row = firstRow; row < values.length; row++) {
if (!values[row].join(""))
return (row + 1);
}
return null;
}
Here is what it'll look like:

How do I bold one line in a Google Docs Script?

I'm writing a script to parse a Google Sheet and format the cells nicely on a Doc. I'd like the cell data from column 1 to always be bold and the cell data from column 6 to always be Italic. The problem is, after appending a paragraph to the document body, the attribute changes are applied to the entire document. Is there a way to bold/italicize the cell data before appending it to the doc body?
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var doc = DocumentApp.create("Smogon Formatted");
var docBody = doc.getBody();
for (var i = 2; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++){
var cellData = rows.getCell(i, j).getValue()
// Format data based on column
if (j == 1) {
docBody.appendParagraph(cellData).editAsText().setBold(true);
} else if (j == 2 || j == 3) {
var imgFormula = rows.getCell(i, j).getFormula();
var imgUrl = getImageUrl(imgFormula);
docBody.appendParagraph("[img]" + imgUrl + "[/img]");
} else if (j == 6) {
docBody.appendParagraph(cellData).editAsText().setItalic(true);
} else {
docBody.appendParagraph(cellData);
}
}
}
};
EDIT: Try #2, using the setAttributes method
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var doc = DocumentApp.create("Smogon Formatted");
var docBody = doc.getBody();
for (var i = 2; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++){
var cellData = rows.getCell(i, j).getValue()
// Format data based on column
if (j == 1) {
docBody.appendParagraph(cellData).setAttributes(style1);
} else if (j == 2 || j == 3) {
var imgFormula = rows.getCell(i, j).getFormula();
var imgUrl = getImageUrl(imgFormula);
docBody.appendParagraph("[img]" + imgUrl + "[/img]");
} else if (j == 6) {
docBody.appendParagraph(cellData).setAttributes(style2);
} else {
docBody.appendParagraph(cellData);
}
}
}
};
// Style definitions as global variables
var style1= {};
style1[DocumentApp.Attribute.BOLD] = true;
var style2= {};
style2[DocumentApp.Attribute.ITALIC] = true;
If you use style attributes you can assign a style to every paragraph very easily, you can actually do it for any document element...
Here is a basic example code to show how it works :
(doc here)
function exportToDoc(){
var doc = DocumentApp.openById('16i----L53WTDpzuLyhqQQ_E');// or create a new doc (but not while you test it :-)
var body = doc.getBody();
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
for (var i in values){
var rowData = values[i].join(' + ');
if (i == 1) {
body.appendParagraph(rowData).setAttributes(style2);
} else if (i == 2 ) {
body.appendParagraph(rowData).setAttributes(style1)
}
}
doc.saveAndClose();
}
// Style definitions as global variables
var style1 = {};// style example 1
style1[DocumentApp.Attribute.FONT_SIZE] = 10;
style1[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.CONSOLAS;
style1[DocumentApp.Attribute.FOREGROUND_COLOR] = "#444400";
var style2 = {};// style example 2
style2[DocumentApp.Attribute.FONT_SIZE] = 16;
style2[DocumentApp.Attribute.FONT_FAMILY] =DocumentApp.FontFamily.ARIAL_NARROW;
style2[DocumentApp.Attribute.FOREGROUND_COLOR] = "#005500";
//
example random data result :

Categories

Resources