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:
Related
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++;
}
})
}
function writeData () {
var sss= SpreadsheetApp.getActiveSpreadsheet();
//var inSheet2 = sss.getSheetByName('Copy of 01-Raw Data 2');
var inSheet2 = sss.getSheets()[4];
var input2 = inSheet2.getRange(2, 1, inSheet2.getLastRow(),42);
var outSheet1 = sss.getSheets()[0];
var outStartRow2 = outSheet1.getLastRow() + 2;
var outStartRow3 = outSheet1.getLastRow() + 2;
input2.copyFormatToRange(outSheet1, 2, 42, outStartRow3, outStartRow2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inSheet = sss.getSheets()[0];
var input = inSheet.getRange(2, 18, inSheet.getLastRow(), 42).getValues();
var outSheet2 = sss.getSheets()[4];
var outStartRow = outSheet2.getLastRow() + 1;
for (var col = 0; col < input.length; col++) {
for (var row = 0; row < input[col].length; row++) {
outSheet2.getRange(outStartRow + row, col + 5)
.setValue(input[col][row]);
}
}
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Write data", functionName: "writeData"});
ss.addMenu("Custom functions", menuEntries);
}
I am using google-apps-script in Google spreadsheets.
What i have:
What I want to achieve:
Please, let me know if you got a way to make this correctly.
The Code:
function myFunction() {
var ss=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('INPUT');
var sh1=ss.getSheetByName('OUTPUT');
var rg0=sh0.getDataRange();
var vA=rg0.getValues();
var oA=[];
for(var i=0;i<vA.length;i++)
{
for(var j=0;j<vA[0].length;j++)
{
if(vA[j][i])
{
oA.push([vA[j][i]]);
}
else
{
for(var k=0;k<j;k++)
{
oA.pop();
}
break;
}
}
}
sh1.getRange(1,1,oA.length,oA[0].length).setValues(oA);
}
Data Sheet:
Output Sheet:
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();
}
The goal I am trying to achieve is to retrieve a cell value based on a form radio selection and update a text area.
Process: User opens dialog box. They select a field office. Onclick runs the function check. After check runs google.script.run.withSuccessHandler(addSignatureLine).getSignatureLine(cellElement); is supposed to run and update the textarea with the Id 'AdditionalMessage' with the signature line retrieved from .getSignatureLine.
Here are two functions of the html code:
<script>
function addSignatureLine(signatureLine){
document.getElementById('AdditionalMessage').value = '\n\n'signatureLine;
};
function updateSignatureLine() {
var cellElement = document.getElementById('ET');
console.log('cellElement: ' + cellElement);
google.script.run.withSuccessHandler(addSignatureLine)
.getSignatureLine(cellElement);
};
function check() {
var ele = document.getElementsByName('fieldOfficeET');
var flag = 0;
for (var i = 0; i < ele.length; i++) {
if (ele[i].checked)
flag = 1;
}
if (flag == 1)
document.getElementById('Submit').disabled = false;
};
</script>
Here is the getSignatureLine.gs script
function getSignatureLine(cellObject) {
var ss = SpreadsheetApp.openById('googleSheetId');
var sheet = ss.getSheetByName('AMS Contact Information');
var firstRow = 2;
var lastRow = 10;
var dataRange = sheet.getRange(firstRow, 1, lastRow, 11);
var dataValues = dataRange.getValues();
for (var key in cellObject) { //Loop through all the data in the form
Logger.log('key: ' + key);
Logger.log('value: ' + cellObject[key]);
}
//Determines the row the Field Office is in
for (var rr = 0; rr < dataValues.length; rr++) {
if (dataValues[rr][0] == cellObject.fieldOfficeET) {
var r = rr + 2
break;
}
}
var signatureLine = sheet.getRange(r, 11).getValue();
Logger.log("signatureLine: " + signatureLine)
return signatureLine;
}
There is a problem with this line:
document.getElementById('AdditionalMessage').value = '\n\n'signatureLine;
I would try:
document.getElementById('AdditionalMessage').value = '\n\n' + signatureLine;
Add a plus sign to concatenate the text.
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 :