Открыть ссылку на новой веб-странице из HTML в скрипте приложений - PullRequest
0 голосов
/ 16 апреля 2020

У меня есть веб-приложение со скриптом приложений, которое отображает страницу html, чтобы узнать имя проекта. У меня уже есть шаблон электронной таблицы, который нужно скопировать и переименовать с именем проекта, которое отправлено в форме html. Это работает нормально. Я получил электронную таблицу, созданную с именем проекта, введенным на странице html на моем диске.

Мне нужно открыть вновь созданную электронную таблицу на новой вкладке после отправки формы в html.

Code.gs

    function doGet(request) {
  return HtmlService.createTemplateFromFile('Index').evaluate();
}

/* @Include JavaScript and CSS Files */
function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .getContent();
}

/* @Process Form */
function processForm(formObject) 
{


  var destination = DriveApp.getFolderById('Id')
  var ss= DriveApp.getFileById('Id').makeCopy(formObject.project_name, destination)

  var url= ss.getUrl()

 var html2 = "<script>window.open('" + url + "');google.script.host.close();</script>";
 var userInterface = HtmlService.createHtmlOutput(html2);
 userInterface.setTitle('Creating New Estimate')
 return userInterface

}

Указатель. html

<!DOCTYPE html>
<html>
    <head>
        <base target="_top">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <?!= include('JavaScript'); ?>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-6">
                    <form id="myForm" onsubmit="handleFormSubmit(this)">
                        <p class="h4 mb-4 text-left">Creating a New Estimate</p>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="project_name">Please enter the Project Name</label>
                                <input type="text" class="form-control" id="project_name" name="project_name" placeholder="Project Name" required>
                            </div>

                        </div>

                        <button type="submit" class="btn btn-primary btn-block">Submit</button>

                              <p><a href="webpage link">Click Here</a>  to go back to the proposal management site.</p>

                    </form>
                    <div id="output"></div>
                </div>
            </div>      
        </div>
    </body>
</html>   

JavaScript. html

    <script>
  // Prevent forms from submitting.
  function preventFormSubmit() {
    var forms = document.querySelectorAll('form');
    for (var i = 0; i < forms.length; i++) {
      forms[i].addEventListener('submit', function(event) {
      event.preventDefault();
      });
    }
  }
  window.addEventListener('load', preventFormSubmit);    


  function handleFormSubmit(formObject) {

    google.script.run.processForm(formObject);
    document.getElementById("myForm").reset();

 }

</script>

Предложение, пожалуйста .

Ответы [ 2 ]

1 голос
/ 16 апреля 2020

Попробуйте использовать этот подход:

google.script.run
.withSuccessHandler(function(){
  //Go back and access the spreadsheet here
})
.processForm(formObject);
0 голосов
/ 16 апреля 2020

Получить URL-адрес электронной таблицы. Используйте URL-адрес в приведенной ниже функции, чтобы открыть ее на другой вкладке в том же окне браузера.


//
//
function openUrl( url ){
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}
//
//
function open_help(){
 openUrl("https://docs.google.com/document/d/1zDY/edit");
}
//
//


...