Xcode 7 insert a new line and go to - xcode7

Is there a keyboard shortcut to go to a newline regardless of where is the cursor currently at?
notice the cursor before the last closing " and want to go to a newline in a keyboard shortcut.
var myName = "Jeff|"
var age = "55"
expected
var myName = "Jeff"
| <--- new line
var age = "55"
thx

Related

Google Apps Script - Format value with 4 digits

I have a variable which needs to be formatted into a 4 digit format,
i.e. 4 would be 0004, or 38 would be 0038
I've tried using getNumberFormat as below but this gives me no luck. Looking around I'm struggling to find a clean solution to this?
var stringVal = 38;
var prettyVal = stringVal.getNumberFormat('0000');
worksheet.getRange(row+1, AUTOINC_COLUMN+1).setValue("GL"+prettyVal);
EDIT - It needs to be formatted before being placed into my spreadsheet - since I'll append the string 'GL' before the value to form the key
To achieve expected result, use below option
var stringVal = 38;
var format = "0000"
var prettyVal = stringVal.toString().length < 4 ? format.slice(stringVal.toString().length) + stringVal.toString() : stringVal ;
console.log(prettyVal)
I'm assuming you are referring to a spreadsheet. Once you have placed the value in a cell or cells you can setNumberFormat.
var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1");
range.setValue(38);
range.setNumberFormat("000#");

google doc script, search and replace text string and change font (e.g., boldface)

i am new to google doc script.
in a google doc, i need to search for several text strings (e.g., "student 1" in lightface font) to replace these text strings with another text string (e.g., "Student A"), but in boldface font.
to search and replace, i use the following code:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
// change "student 1" to "Student A" in boldface
body.replaceText("student 1", "Student A");
}
the above code only replaces "student 1" with "Student A" using the current font of google doc, but i don't know how to change the font from lightface to boldface.
i tried
body.replaceText("student 1", "<b>Student A</b>");
of course, the above code did not work.
any help would be much appreciated. thank you.
a pedestrian way to replace a text string (e.g., "student 1") that has many occurrences in a google doc by a new text string (e.g., "Student A") in boldface, is two steps:
1- write a function (called, say, docReplace) to do a search and replace in regular / normal font (no boldface):
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
// change "student 1" to "Student A"
body.replaceText("student 1", "Student A");
}
2- write a function (called, say, boldfaceText) to do a search for the desired text (e.g., "Student A") and the two offset values for this text (i.e., startOffset and endOffsetInclusive) at each occurrence to set the font for the characters within these offset values to boldface:
function boldfaceText(findMe) {
// put to boldface the argument
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText(findMe);
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Where in the Element is the found text?
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
// Change the background color to yellow
foundText.setBold(start, end, true);
// Find the next match
foundElement = body.findText(findMe, foundElement);
}
}
the above code for boldfaceText was inspired from that in the post Finding text (multiple times) and highlighting.
an offset value for a character is simply the integer that describes the location of that character in the document, with the very first character having the offset value 1 (it's like the coordinate of the character).
use "Student A" as argument for a call to the function boldfaceText, i.e.,
boldfaceText("Student A");
which could be embedded into the function docReplace, i.e.,
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
// change "student 1" to "Student A"
body.replaceText("student 1", "Student A");
// set all occurrences of "Student A" to boldface
boldfaceText("Student A");
}
in the google doc, simply run the script docReplace to change all occurrences of "student 1" into "Student A" in boldface.
the above two functions (docReplace and boldfaceText) could be a good way to introduce newbies (like me) to google doc scripts. after some time playing with google doc scripts to gain some familiarity, learn Robin's more elegant and advanced code that does the above two steps all at once.
Replacing one string by the other could be done with an easy find and replace.
Setting them bold is a bit more difficult as you have to get the text element of the body and also find the start and end position of every occurrence the word.
Since regular expressions in JS don't return an array of matches but instead an array of properties of the current match you have to loop through the function, which always starts after the last match.
function docReplace(input, output) {
var re = new RegExp(output,"g");
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(input, output);
var text = body.getText();
var index;
while(true){
index = re.exec(text)
if(index == null){break}
body.editAsText().setBold(index.index, output.length + index.index, true);
}
}

Missing ; before statement Google Apps Script

I am new to Javascript, my error is Missing ; before statement on Line 25.
The other question was Java related, not JavaScript related.
Here is the code that is problematic.
Line 24 function emailChecker(){
Line 25 var readMessages() = DocumentApp.create();
Line 26 var emailAddress() = Session.getActiveUser().getEmail();
Line 27 GmailApp.getInboxUnreadCount();
Line 28 var unreadMessages() = InboxUnreadCount();
Line 29 GmailApp.sendEmail(emailAddress) (You have + unreadMessages + unread messages!)
}
The problem is at var readMessages() = .... when you write var x = something;, you are assigning something to the variable x. Brackets are used for functions, not variables. Try this instead:
var readMessages = DocumentApp.create();

Insert date time in google document

I would like to insert a timestamp (date and/or time) into Google Documents. The support documentation () says that there should be a keyboard shortcut, but it does not work in my environment (Win7 + IE9).
Can anyone provide me with a Google Apps script to achieve this?
This works well
In Google Docs : Tools -> Open Script Editor and save this script
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Custom Menu')
.addItem('Insert Date', 'insertDate')
.addToUi();
}
function insertDate() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var d = new Date();
var dd = d.getDate();
dd = pad(dd, 2)
var mm = d.getMonth() + 1; //Months are zero based
mm = pad(mm, 2)
var yyyy = d.getFullYear();
var date = dd + "-" + mm + "-" + yyyy;
var element = cursor.insertText(date);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
Reload the Doc, Accept the permissions.
I am not sure if an add-on falls under the category Google Apps Script you were asking for, bug Text Factory provides the feature to insert a time-stamp.
Here is an edited version (of the one already provided) that prints out a date stamp including the time.
Here is an example of the output: 2:43:21 AM EST 26-03-2014
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Insert Date')
.addItem('Insert Date', 'insertDate')
.addToUi();
}
function insertTime() {
var d = new Date();
var timeStamp = d.getTime(); // Number of ms since Jan 1, 1970
// OR:
var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
}
function insertDate() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var d = new Date();
var dd = d.getDate();
dd = pad(dd, 2)
var mm = d.getMonth() + 1; //Months are zero based
mm = pad(mm, 2)
var yyyy = d.getFullYear();
var timeStamp = d.getTime(); // Number of ms since Jan 1, 1970
var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
var date = currentTime + " " + dd + "-" + mm + "-" + yyyy;
var element = cursor.insertText(date);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
If you want to get automatically current date after open document, you can add this script:
In Google Docs: Tools -> Open Script Editor and save this script:
/**
* After open document actualize part with text "Generated" to "Generated [actual date]".
*/
function onOpen() {
var body = DocumentApp.getActiveDocument().getBody();
var date = Utilities.formatDate(new Date(), "GMT", "dd.MM.yyyy");
// Clear the text surrounding "Apps Script", with or without text.
body.replaceText("^Generated.*$", "Generated " + date);
}
In the document body you must have text "Generated".
Create a new Sheets document
In A1, put Date: and B1, put the formula NOW(). I named the sheet current-date, but you can name it whatever you want. This document will simply sit on your drive to server as a "date keeper" for all of your Docs documents.
Format Cell as Date
Select cell B1 and choose Number >> Date from the Format menu.
Select Cells and Copy
Select cells A1 and B1, right click, and choose Copy
Paste and Link to Docs Document
Right click in your Docs document where you want to paste the cells and click Paste. Docs should ask you if you want to link these cells to the source document. Select Link to spreadsheet and then click Paste.
Get Rid of the Borders
To get rid of the borders, right click on the inserted cells and select Table Properties.
Now set Table Border to 0pt and click OK.
Final Result
You should end up with something like this. You can drag the edges of the cells to make them bigger or smaller and change the font and text size of the cells as well. If you make the text bigger, the text will wrap inside the cells so you will need to make them wider.
Updating the Date
Now whenever you open your document with the linked cells and the date has changed, you should see this. Click Update and your date will be updated to today's date. There is no need to ever open the current-date spreadsheet!
Enjoy!
For Docs, you are probably out of luck, as there appears to be no hotkey for that, and support for scripting from within Docs is lacking (Spreadsheets would be a different story). Since you're on Windows, you can avail yourself of autohotkey, though. This video, while way too long, shows assigning a global hotkey combo to insert the current date anywhere. With that, you can insert your date/time anywhere you want to while using your Windows system. (You can customize it to be only active in certain applications, such as IE, if you want to get wacky with it)
Because extra key-strokes are being used to insert the date from the menu, my solution is a batch file, sts.cmd, that stores the date-time into the clip-board when called, enabling an easy Windows+R, sts, Ctrl+V to get and paste. If you are adding extra keystrokes to get the date into the doc anyway you might as well just paste it in.
The code is
#ECHO OFF
for /f "tokens=1-12 delims=/:. " %%d in ("%time%") do SET MYTIME= %%d:%%e:%%f
for /f "tokens=1-12 delims=/:. " %%d in ("%date%") do SET MYDATE= %%g-%%e-%%f
SET MYTS=%MYDATE%%MYTIME%
ECHO | SET /p dummyname=%MYTS%|clip
This works for me until GDocs comes up with an embeddable function that will update the display date to the current date each time the doc is opened.
On MacOS, I used Automator.
I created a Service that works with Chrome, takes no input, and runs a shell script and sends the output to the clipboard. The shell script is very basic:
DateTime=`date "+%Y-%m-%d %H:%M"`
echo $DateTime
I could have done without the intermediate variable and just run the date command, but I had the idea that I might want to do extra processing and formatting. Note that if you want to include spaces in your formatting of the date string, the formatting argument must be in quotes. The screen shot uses an underbar instead.
With the script saved, I went to System Settings > Keyboard > Shortcuts, I found my script DateTime2CB.workflow in the Text section and gave it a hotkey. There's no easy way of finding out what hotkeys are in use.
I could have made it for any app (and if I find myself trying to use it other apps I still might do that), but for now it's Chrome only. I could also have had the shell script pipe the output of date to the clipboard, pbcopy on Mac, as above (using "clip"). I've tested both methods and they work.
I added the same script to another Mac and it was set up to insist that the Automator file (name.workflow) be saved in iCloud. in that context, it was not listed in the Keyboard settings. To fix that, I had to reopen the saved iCloud file in Automator using Open > Recent. It asked if I wanted to install the file in Services. A yes answer at that point saved it on my local system and I could find it under Settings > Keyboard > Shorcuts > Services.
I hope this helps other Mac users.
August
Try this:
function insertTimestamp() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getActiveCell();
cell.setValue(new Date());
// sets the cells format to month/day/year.
// Remove if you want time inserted as well
cell.setNumberFormat("MM/dd/yyyy")
}
This works on spreadsheets not sure if you were looking for other docs.
Create a Spreadsheet Today in google docs. Put the date in B1 as Today() Format it in C1 using the function Text(B1,"dddd, mmmm d, yyyy")
Then use the following script (you will need the url of the Today Spreadsheet
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body1 = doc.getBody();
var style1 = {};
style1[DocumentApp.Attribute.BOLD] = true;
var text1 = doc.editAsText();
body1.appendHorizontalRule();
var wb = SpreadsheetApp.openByUrl('spreadsheet url');
var ss = wb.getSheetByName('Today');
var r = ss.getRange('C1');
var date1 = r.getValues();
var par1 =body1.appendParagraph(date1[0]);
par1.setAttributes(style1);
}

AS3: Remove and add to variable after a character

I am pulling data from a php script that gives me the names of a person on facebook along with there ID in this format:
Person Name:123456789
I would like to know if there is a away to split after the ":" and add the number (ID) to on array and the person name to another array.
Thanks for any help
Eli
Something like this:
var personArray:Array = [];
var idArray:Array = [];
var stringToSplit:String = "Person Name:123456789";
var splitArray:Array = stringToSplit.split(":");
personArray.push(splitArray[0]);
idArray.push(splitArray[1]);
trace(personArray); // outputs "Person Name"
trace(idArray); // outputs "123456789"