Vertically Align Text on google spreadsheet via google script - google-apps-script

I am trying to figure out how to align text in the middle vertically. According to the documentation I have found my following example should work:
var form = FormApp.getActiveForm();
var ss = SpreadsheetApp.openById(form.getDestinationId());
var sheet = ss.getSheets()[0];
var fullRange = sheet.getRange("A1:Z1001");
fullRange.setHorizontalAlignment(DocumentApp.HorizontalAlignment.CENTER);
fullRange.setVerticalAlignment(DocumentApp.VerticalAlignment.CENTER);
The interesting thing I find is that the Horizontal Alignment works, but the vertical one does not. Am I possibly using out dated methods? Is there a better way to accomplish this?

Just use middle as the parameter for setVerticalAlignment() :
fullRange.setHorizontalAlignment("center").setVerticalAlignment("middle");
You're using the Enum for the DocumentApp intended for Google Docs tables and cells and you're using Spreadsheets.

Related

Can we hide column in sheet using google apps script?

I want to share the sheet with others but i also want them to not able to see some columns. Is there any way out to solve this using google apps script or if any alternate option for the same. Thank you.
You should take a look at the documentation, it will help you to find the function you're looking for. In this case, you need the Sheet.hideColumns(columnIndex) method
hideColumns(columnIndex)
Hides the column at the given index.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first column
sheet.hideColumns(1);
Very simple function
function onOpen(){
ss = SpreadsheetApp.openById( 'sheetid' ),
sheet = ss.getSheetByName('sheet_name'),
ss.getActiveCell();
sheet.hideColumns(start_column_number, end_column_number);
}
If the columns to be hidden are part of a merged column set, the hide columns methods don't work unless you hide all the columns in the merge.

Google Script different aligment in same table row in Docs table

I'm trying to put three paragraphs (loaded from google spreadsheet) into docs via google app script.
One should be on its own row and the other two should be on same row but with different alignment.
Pic. 1 - What I want
The problem is, google allow only appending paragraphs into table cells. And paragraph contains new line so the second text is on the new line.
Pic. 2 - What I get
I've tried appending paragraph and then appending text.
But I don't know how to set up right-aligned tab and insert tab after first text.
Is it even possible to set up tabs using google script only ?
I welcome any help or suggestion how can I create text as showed in Pic. 1. Than you for any help.
There is actually an issue (3211) that prevents doing exactly what you want : the merge() method is causing the doc crash.
so the code below will result in something like this :
because we cannot merge the 2 cells in the first row from the script.
Doing it manually works well :
code :
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.clear();
var table = body.appendTable([['',''],['','']]);
table.setBorderColor('#ffffff');
table.getCell(0,0).appendParagraph('Some important text important text important text important text ');
//table.getCell(0,1).merge();// this is the issue...it crashes the doc
table.getCell(1,0).appendParagraph('left text').setAlignment(DocumentApp.HorizontalAlignment.LEFT);
table.getCell(1,1).appendParagraph('right text').setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
doc.saveAndClose()
}

How to remove data validations?

I want to remove all data validations from a sheet, tried following but it does not get rid of them. Anyway to do this through apps script?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var accountSheet = ss.getSheetByName("Account A");
accountSheet.getRange(1,1,274,61).clear();
accountSheet.getRange(1,1,274,61).setDataValidation(null)
That should do the job.
A more generic answer to remove all data validations from the whole sheet (remove from all cells in sheet):
mySheet.getRange(1,1, mySheet.getMaxRows(), mySheet.getMaxColumns()).setDataValidation(null);
You could in theory use this less verbose version (might be faster because it affects a smaller range of cells):
mySheet.getDataRange().setDataValidation(null);
However, there may be empty cells outside the DataRange that do have validations and those validations will not be removed (they are outside the scope of DataRange, won't be selected by DataRange). Hence, you should use the first version I mentioned for more robust code: it selects all the cells in the sheet and removes validations.

Align multiple images beside in Google Doc

after several hours of searching and try & error, I have to give up.
I got a google doc wich I fill up with some data from a google sheet.
Also I create some pie charts with the data and would like to align 2 (or even more) little charts (>200x200 px) inline beside eachother.
But I am not able to do it. :(
One single image, or chart isn´t any problem, but 2 or more seems to be pretty hard.
Thats why I picked up this option and Your experience as my last option.
Thank you very much for every hint and have a nice Day.
Michael
"Short" Code Sample:
Step 1: Open Google Sheet
var reportDataStorage = SpreadsheetApp.openByUrl('https://docs.google.com/.../edit');
var sheet = reportDataStorage.getSheetByName(...);
Step 2: Create Table
var daten_besucher_nach_quelle = Charts.newDataTable()
...
daten_besucher_nach_quelle.addRow([sheet.getRange('O'+y).getValue(), sheet.getRange('P'+y).getValue(), sheet.getRange('Q'+y).getValue(); sheet.getRange('R'+y).getValue()]);
...
daten_besucher_nach_quelle.build();
Step 3: Create Chart from Table
var chart_besucher_nach_quelle = Charts.newPieChart()
.setDataTable(daten_besucher_nach_quelle)
.setDimensions(300, 300)
...
.build();
Step 4: Append Image
body.appendImage(chart_besucher_nach_quelle.getAs('image/png'));
Is this enough?
As you noticed, when you insert an image in a document, a new paragraph is automatically added to the document's body.
This paragraph container is accessible using image.getParent() method. As soon as we get this paragraph we can add another image in it, this image will be directly aligned side by side (as long as the total width is less than the maximum size between margins).
Try this small simple code and see the result in the scren capture below :
function importImageTest(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var image1 = DriveApp.getFileById("0B3qSFd3iikE3TUFFLWY0ZWJjNzM2LWFiNDYtNGU1OC1hMGNhLTViM2UxNTBlMTE3Nw");// some image in my drive
var image2 = DriveApp.getFileById("0B3qSFd3iikE3TUFFLWI3Y2JiOTU1LWM0OGYtNDVkMS05ZTRiLTkzNjQ5NDBlZGFkNA");
var firstImage = body.appendImage(image1).setWidth(150).setHeight(100);// an arbitrary size
var currentParagraph = firstImage.getParent();// the container of first image
currentParagraph.asParagraph().appendInlineImage(image2).setWidth(150).setHeight(100);// add the second image to this parent paragraph
doc.saveAndClose();
}

Bold conditional formatting script for Google Spreadsheets

What I want to do is essentially what this user wanted to do here:
I need a script that formats the cells in column A bold, but only the cells that contain the word 'Hello'.
However I have no knowledge of Google Apps scripts at all, and I need an answer put in much simpler terms than what I could find there or anywhere else. Any help is appreciated; thank you!
To start, from your spreadsheet, open "Tools / Script Editor...". When the dialog opens, choose to "Create Script For... Spreadsheet". You will end up with a sample script - we're going to edit it to do what you want.
Change the readRows() function as shown here. The change is that instead of logging the content of every row, we will use an if statement to check if the cell contains a string with 'Hello' in it. Then, if it does, we'll bold the cell text.
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
// Arrays start at 0, Google Sheets start at 1 - must remember that.
// We will loop starting at 1, because we want to skip the header in
// Row 1, aka Array index 0
for (var i = 1; i <= numRows - 1; i++) {
var colA = values[i][0];
if (colA.toString().indexOf('Hello') >= 0) {
sheet.getRange(i+1,1).setFontWeight("bold");
}
}
};
Now, how to run that? The sample already has an onOpen() function that will set up a custom menu... let's just change the text it displays in the User Interface, as shown here. The only change is in the 'name' property of the menu entries.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Bold Hello",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};
Save your script. Go back to your spreadsheet, and reload it (to get it to run the onOpen trigger function). When your menu shows up, you're all set.
Next - start with the "First Script" tutorial here. The Google Apps Script documentation covers all the services provided by Apps Script, but the basic language structure and objects are javascript, so you should get familiar with that. Just try googling "learn javascript", and you'll find tons of tutorials, books, and other resources.
I can't make this simpler.
In the now not so new 'New' Sheets this can be achieved without a script:
Clear formatting, select ColumnA and Format, Conditional formatting..., Format cells if... Text contains and:
hello
Then for Formatting style click the B and Done.
This way is not case sensitive and will embolden contents such as OTHELLO.
If you aren't trying to set too many conditional formatting rules, there's an easier way to set colors, though not bold. In Google Drive Spreadsheet, click the "Format" menu. The bottom menu item should be "Conditional formatting..."; click that. That should produce a dialog box that defaults to something like this (to the extent that I can draw it with text):
x
Conditional formatting
[Text contains ◊ ] [ ] []Text: [ ] []Background: [ ] x
e.g. "done" or "Jonathan"
_______________________________________________________________________________
+ Add another rule
[ Save rules ] [ Cancel ]
In your example, you're looking for cells that contain "Hello", so the default of "Text contains" would do the job. Put "Hello" into the text box, and set a format in the "Text":" and "Background:" boxes. That doesn't give you bold, but it does allow colors.
I see that your question dates back half a year, so it's probably too late for you (and if you strictly need bold, it doesn't solve the problem anyway), but it may help others.