Add New Style To Google Docs

In order to enable it you need to create a new script: Tools >> Script Editor>> New and paste the code there. Then you will have to authorize the script and reload the document so that the menu appears on in the menu bar. Then select a patch of text and pick Extras >> Apply code style. The following code snippet was taken from here- http://pastebin.com/4pNBJ9pC

// is called by google docs when a document is open
// adds a menu with a menu item that applies a style to the currently selected text
function onOpen() {
  DocumentApp.getUi()
  .createMenu('Extras')
  .addItem('Apply code style', 'applyCodeStyle')
  .addToUi();
}
 
// definition of a style to be applied
var style = {
  bold: false,
  backgroundColor: "#DDDDDD",
  fontFamily: DocumentApp.FontFamily.CONSOLAS
};
 
// helper function that strips the selected element and passes it to a handler
function withElement(processPartial, processFull) {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
    var elements = selection.getSelectedElements();
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];
      if (element.getElement().editAsText) {
        var text = element.getElement();
        if (element.isPartial()) {
          var from = element.getStartOffset();
          var to = element.getEndOffsetInclusive();
          return processPartial(element, text, from, to);
        } else {
          return processFull(element, text);
        }
      }
    }
  }
}
 
// called in response to the click on a menu item
function applyCodeStyle() {
  return withElement(
    applyPartialStyle.bind(this, style),
    applyFullStyle.bind(this, style)
  );
}
 
// applies the style to a selected text range
function applyPartialStyle(style, element, text, from, to) {
  text.setFontFamily(from, to, style.fontFamily);
  text.setBackgroundColor(from, to, style.backgroundColor);
  text.setBold(from, to, style.bold);
}
 
// applies the style if the entire element is selected
function applyFullStyle(style, element, text) {
  text.setFontFamily(style.fontFamily);
  text.setBackgroundColor(style.backgroundColor);
  text.setBold(style.bold);
}
Advertisements

Auto complete search textbox like google (multiple input & extra data)

I have posted another article like this. https://programmershandbook.wordpress.com/2015/11/16/auto-complete-search-textbox-like-google-in-a-simple-easy-way/.

But a different approach is used here. Previous article has single textbox with id selector and only the textbox text. But this time, we have multiple textboxes with class selectors and few extra data in the ajax request.

<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AutoComplete Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
     
</head>
<body>
   <table data-employeeid="5">
     <tbody>
       <tr data-id="546">
         <td>
           <input type="text" class="suggestexam" />
          </td>
        </tr>
        <tr data-educationid="103">
         <td>
           <input type="text" class="suggestexam" />
          </td>
        </tr>
      </tbody>
    </table>
</body>
</html>


//Put this code in head tag (placing this at the bottom of body tag may not work). 
    $(function () {
        $('body').on('focus', ".suggestexam", function () {
            var extra = $(this).closest('tr').attr("data-id");
            $(this).autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/Employee/GetExamNameSuggestion",
                        data: JSON.stringify({ prefix: request.term, extradata: extra }),
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        //dataFilter: function (data) { return data; },
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                },
            });
        });
    });

Auto complete search textbox like google in a simple & easy way.

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AutoComplete Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    
</head>
<body>
    <div class="demo">
        <div class="ui-widget">
            <label for="tbAuto">Enter UserName: </label>
            <input type="text" id="txtSearch" class="autosuggest" style="width:300px;" />
        </div>
        </div>
</body>
</html>

Place the script in head tag

<script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "/Home/GetAutoCompleteData",
                        data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
    </script>
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAutoCompleteData(string username)
        {

            List<string> result = new List<string>();

            result.Add("Hello");
            result.Add("World");
            return Json(new { d = result }, JsonRequestBehavior.AllowGet);
        }
    }