82288

Google apps script and script db replacement example needed

Question:

I've got a google apps script UI I am using in a google doc.

I'm trying to replace the current handler which uses the Script DB. Script DB has since been deprecated. The amount of information I was writing was minimal and I figured I would just write the info to google sheets.

Here is the handler from the .html

function addApprover(){ google.script.run.withSuccessHandler(function() { getApprovers(); $('#approver').val(''); }).addApprover($("#approver").val()); }

.gs

function addApprover(email){ var db = ScriptDb.getMyDb(); var docId = DocumentApp.getActiveDocument().getId(); var ob = { docId: docId, approverEmail: email, status: null, emailSent: false } db.save(ob); var history = { docId: docId, action: 'Added Approver', email: email, date: Utilities.formatDate(new Date(), "GMT", "MM-dd-yyyy' 'HH:mm:ss"), } db.save(history); }

I figure that I still call the .gs function and just need to change the function accordingly.

I'm fairly certain that the text box approver holds the email addresses.

How do I access these items?

I'm fairly certain I'm looking for a "for each" statement to iterate through each email address and send them a message and write their name to a specific area of a sheet but I am unsure how to proceed.

Answer1:

Hopefully this will get you started:

function addApprover(email){ var docId = DocumentApp.getActiveDocument().getId(); var ss = SpreadsheetApp.openById('Your Spreadsheet file ID here'); var sheetToWriteTo = ss.getSheetByName('Your sheet name here'); var rowData = [docId, email, null, false]; sheetToWriteTo.appendRow(rowData); var history = [docId, 'Added Approver', email, Utilities.formatDate(new Date(), "GMT", "MM-dd-yyyy' 'HH:mm:ss")]; sheetToWriteTo.appendRow(rowData); }

If you want to write the two sets of data to two different sheets, you'll need to get a reference to a second sheet. The data goes into an array, not an object. Although you will see an array called an object in Google documentation also. If you see brackets [], it's an array.

If you have any problems, debug the code with Logger.log() statements and/or <strong>debug</strong> and a breakpoint, then post another question if it's a major issue, or if it's something minor, make a comment here.

Recommend

  • How to Set Horizontal Alignment On A Table In Apps Script
  • How can I select and copy a chart from a spreadsheet to a doc using Google Script?
  • How to add a hyperlink in a Google Docs using a Google Script
  • Google Apps Script---Replace Straight Quotation Marks with Curly Ones
  • run time annotation scanning when Dynamic class loading
  • playing sound with google script
  • Insert an element (table) to the cursor position
  • Stringify object name in Lua
  • Capture literal brackets inside brackets in pyparsing
  • How to parse nested JSON with GSON
  • What should be the grok pattern for thoses logs ? (ingest pipeline for filebeat)
  • How to Set wallpaper of image with its actual size?
  • Mapping ManyToMany with composite Primary key and Annotation:
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • FlexJSON Orders Alphabetically by Default
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Google Calendar Api is not showing event list
  • Encrypting credit card details using AngularJS in Braintree
  • How to access recipient on sent messages page with mailboxer
  • Dynamically set LESS variables from user settings
  • testing a POST using phpunit in laravel 4
  • Can't remove headers after they are sent
  • 550 Access denied - Invalid HELO name
  • Login not working in Firefox in Meteor
  • How to use JavaScript to determine whether a file exists in a directory?
  • How do I access an unhandled exception in an MVC Error view?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Email verification using google app script and google forms
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Email format validation in mvc3 view
  • sending/ receiving email in Java
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Turn off referential integrity in Derby? is it possible?
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?