如何将Google App脚本发布为Web App并从Google Sheet调用

时间:2019-06-02 14:00:14

标签: google-apps-script web-applications google-sheets

我有一个脚本,可以修改带有受保护字段的某些工作表。无论是否登录,使用该Google表格的任何人都必须运行此脚本。在试图弄清楚这一点时,有人建议将脚本发布为Web App并允许任何人运行它。我这样做了,但是我不确定如何从Google工作表中调用网络应用。

因此,我将该脚本作为Web应用程序发布,然后尝试调用URL。我的脚本如下:

function doGet(e){
  confirm(); // This is your function that writes to the protected sheet
}

function confirm(){
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert('This will submit the timesheet. Do you want to continue?', ui.ButtonSet.YES_NO);
  if(response == ui.Button.NO) return;
  emailGoogleSpreadsheetAsPDF();
}

/* Email Google Spreadsheet as PDF */
function emailGoogleSpreadsheetAsPDF() {

  // Send the PDF of the spreadsheet to this email address
  var email = "email@gmail.com"; 
  var exclA=['Timesheet','Note','Settings','Data'];//and others
  var timeS=SpreadsheetApp.getActive().getSheetByName('Timesheet')
  var ss=SpreadsheetApp.getActive();
  var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
  var tname=ss.getRange("Timesheet!J6").getValue();
  var agency=ss.getRange("Timesheet!B4").getValue();//same here
  var fldr=DriveApp.getFolderById('abc123thu8h7r8888tbgyru');
  var fA=[];
  var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
  var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name); 
  var body=Utilities.formatString('This was submitted on %s',today);
  var shts=ss.getSheets();

  SpreadsheetApp.flush();//this may not be necessary...not sure
  var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', tname,agency,today));
  fA.push(file)

  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var name=sh.getName();
    if(exclA.indexOf(name)==-1) {
      sh.showSheet();
      for(var j=0;j<shts.length;j++) {
        if(shts[j].getName()!=name) {
          shts[j].hideSheet();
        }
      }
      SpreadsheetApp.flush();//this may not be necessary...not sure
      var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_note.pdf', name,agency,today));
      fA.push(file);
    }
  }
  for(var i=0;i<shts.length;i++) {
    if(exclA.indexOf(shts[i].getName())==-1) {
      shts[i].showSheet();
    }
  }
  timeS.showSheet();

  GmailApp.sendEmail(email,subject,body, {attachments:fA});
  for(var i=0;i<fA.length;i++) {
    fA[i].setTrashed(true); 
  }
  //CopyDataToNewFile();
}

function makeCopy() {
  var ss =SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Note');
  var dSheet = sheet.copyTo(SpreadsheetApp.openById(ss.getId()))
  dSheet.showSheet()
};

function CopyDataToNewFile(targetSheetName,targetSsId, sourceSheetName,sourceSsId) {
  var ss = SpreadsheetApp.openById('gnu84uw84nwutnst9ntrgbrn').getSheetByName('Timesheet');
  var ssd = SpreadsheetApp.openById('h3487g8bg8ybw4gy8wytb').getSheetByName('Sheet1');

  var therapist = ss.getRange('J6').getValues();
  var thedate = ss.getRange('A10').getValues();
  var theagency = ss.getRange('B4:C4').getValues();
  var thepayperiod = ss.getRange('B6:C6').getValues();
  var thecost = ss.getRange('E24').getValues();
  var themileage = ss.getRange('E27').getValues();

  ssd.getRange(ssd.getLastRow()+1,1,therapist.length,therapist[0].length).setValues(therapist);
  ssd.getRange(ssd.getLastRow()+0,2,thedate.length,thedate[0].length).setValues(thedate);
  ssd.getRange(ssd.getLastRow()+0,3,theagency.length,theagency[0].length).setValues(theagency);
  ssd.getRange(ssd.getLastRow()+0,4,thepayperiod.length,thepayperiod[0].length).setValues(thepayperiod);
  ssd.getRange(ssd.getLastRow()+0,5,thecost.length,thecost[0].length).setValues(thecost);
  ssd.getRange(ssd.getLastRow()+0,6,themileage.length,themileage[0].length).setValues(themileage);

}

我在顶部添加了doGet(e)函数,因为我认为这是调用脚本时启动脚本所需要的。

在工作表上,我有一个最初使用的按钮,并使用Assign script选项调用了我的confirm()函数。在将脚本发布为Web应用程序之后,我尝试将URL放在Assign脚本字段中,但是那没有用,然后我尝试仅超链接到Web应用程序,并收到“找不到脚本功能:doGet”错误。

因此,现在我很困惑,不确定如何执行此操作,或者即使这是解决我的问题的最佳方法,希望有更多经验的人可以提供帮助。谢谢!

0 个答案:

没有答案