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

时间:2017-08-31 04:56:55

标签: javascript google-apps-script google-sheets google-drive-api

是否可以在警报后伪装YES按钮,以便在点击时转到刚刚创建的google驱动器文件夹位置,例如

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()

 }

我在代码的最后一行遇到问题,我无法弄清楚如何获得YES按钮,这样当点击时,会打开一个到google驱动器位置的新窗口。

对于上下文,这里是创建唯一文件夹的地方

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 个答案:

答案 0 :(得分:0)

这个示例脚本怎么样?为了使用户重定向到文件夹URL,我建议使用HtmlService。此脚本显示一个带有是/否按钮的对话框。当用户按下是时,创建一个文件夹并移动到用户浏览器中的文件夹。

首先,请运行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();
}
上面脚本中的

HTML

<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);
}