Google apps script and script db replacement examp

2019-06-03 07:29发布

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.

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-03 08:08

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 debug and a breakpoint, then post another question if it's a major issue, or if it's something minor, make a comment here.

查看更多
登录 后发表回答