代码之家  ›  专栏  ›  技术社区  ›  Gramps

单击“是/否”用户界面按钮时重定向到链接

  •  0
  • Gramps  · 技术社区  · 9 年前

    是否可以在警报后隐藏YES(是)按钮,以便在单击时转到刚刚创建的google drive文件夹位置,例如。

    var ui = SpreadsheetApp.getUi();
       var response = ui.alert('Your CSV file has been saved in your Google drive', 'Do you wan to go to that file location?', ui.ButtonSet.YES_NO);
    
       // Process the user's response.
     if (response == ui.Button.YES) {
       var folder = DriveApp.createFolder('test');
       folder.getUrl()
    
     }
    

    我在最后一行代码中遇到了问题,我不知道如何获取“是”按钮,因此当单击时,会打开一个新窗口,指向谷歌驱动器的位置。

    function saveAsCSV() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheets = ss.getSheets();
      var date = SpreadsheetApp.getActiveSheet().getRange(3,2).getValue();
      var time = SpreadsheetApp.getActiveSheet().getRange(4,2).getValue();
      var site = SpreadsheetApp.getActiveSheet().getRange(2,2).getValue();
      // iterate through all sheets in the spreadsheet and rename them according to cell B2
      for( var j = 0 ; j < sheets.length; j++) {
        var sourceSheet = sheets[j];
        // get contents of cell B2
        var newSheetName = sourceSheet.getRange("B2").getValue();
        // rename sheet
        sourceSheet.setName(newSheetName);
      }
      // create a folder from the named SNOWSURVEYS with date
      var folder = DriveApp.createFolder('SNOWSURVEYS' + '_' + date + '_'+ site);
        // append ".csv" extension to the sheet name
        fileName = SpreadsheetApp.getActiveSheet().getName() + ".csv";
        // convert all available sheet data to csv format
        var csvFile = convertRangeToCsvFile_(fileName);
        // create a file in the Docs List with the given name and the csv data
        folder.createFile(fileName, csvFile);
     }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Tanaike    9 年前

    HtmlService . 该脚本显示一个带有“是/否”按钮的对话框。当用户按下yes时,将创建一个文件夹并移动到用户浏览器中的文件夹。

    首先,请运行 openDialog() . 这样,您可以在电子表格上看到一个对话框。

    这是一个示例脚本,如果您想更改样式,请修改它。

    示例脚本:

    function openDialog() {
      var html = HtmlService.createHtmlOutput('<p>Your CSV file has been saved in your Google drive</p><p>Do you wan to go to that file location?</p><form><input type="button" value="yes" onclick="google.script.run.withSuccessHandler(go).getUrl()" /><input type="button" value="no" onclick="google.script.host.close()" /></form><script>function go(url) {open(url, "_blank");}</script>');
      SpreadsheetApp.getUi().showModalDialog(html, 'sample');
    }
    
    function getUrl(){
      var folder = DriveApp.createFolder('test');
      return folder.getUrl();
    }
    

    <p>Your CSV file has been saved in your Google drive</p>
    <p>Do you wan to go to that file location?</p>
    <form>
        <input type="button" value="yes" onclick="google.script.run.withSuccessHandler(go).getUrl()" />
        <input type="button" value="no" onclick="google.script.host.close()" />
    </form>
    <script>
        function go(url) {
            open(url, "_blank");
        }
    </script>
    

    enter image description here

    如果我误解了你的问题,对不起。

    在下面的脚本中,如果具有foldername的文件夹存在,则会打开现有文件夹。如果文件夹不存在,则创建具有foldername的文件夹。为了使用此脚本,请修改 getUrl() .

    function getUrl() {
      var foldername = "test"
      return function(foldername){
          var f = DriveApp.getFoldersByName(foldername);
          return f.hasNext() ? f.next().getUrl() : DriveApp.createFolder(foldername).getUrl();
      }(foldername);
    }
    
    推荐文章