[英]Having YES/NO user interface buttons redirect to a link when clicked
Is it possible to disguise the YES button after an alert so that when clicked it goes to a google drive folder location that has just been created eg 是否可以在警报后掩盖“是”按钮,以便在单击时将其转到刚刚创建的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()
}
I am having trouble with the last line of the code, i can't figure out how to get the YES button, such that when clicked, opens a new window to that google drive location. 我在代码的最后一行遇到了麻烦,我不知道如何获取“是”按钮,因此当单击该按钮时,会为该Google驱动器位置打开一个新窗口。
For context here is where the unique folder is created 对于上下文,这里是创建唯一文件夹的位置
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);
}
How about this sample script? 这个示例脚本怎么样? In order to make users redirect to the folder URL, I propose the use of
HtmlService
. 为了使用户重定向到文件夹URL,我建议使用
HtmlService
。 This script displays a dialog box with yes/no buttons. 该脚本显示一个带有“是/否”按钮的对话框。 When user pushes yes, a folder is created and move to the folder in user's browser.
当用户按“是”时,将创建一个文件夹并将其移至用户浏览器中的文件夹。
At first, please run openDialog()
. 首先,请运行
openDialog()
。 By this, you can see a dialog box on Spreadsheet. 这样,您可以在电子表格上看到一个对话框。
This is a sample script, if you want to change the style, please modify it. 这是一个示例脚本,如果要更改样式,请对其进行修改。
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>
If I misunderstand your question, I'm sorry. 如果我误解了您的问题,对不起。
In following script, If the folder with the foldername is existing, it opens the existing folder. 在以下脚本中,如果具有文件夹名称的文件夹已存在,则它将打开现有文件夹。 If the folder is not existing, the folder with foldername is created.
如果该文件夹不存在,则创建具有文件夹名称的文件夹。 In order to use this script, please modify
getUrl()
. 为了使用此脚本,请修改
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.