[英]Having YES/NO user interface buttons redirect to a link when clicked
是否可以在警報后掩蓋“是”按鈕,以便在單擊時將其轉到剛剛創建的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()
}
我在代碼的最后一行遇到了麻煩,我不知道如何獲取“是”按鈕,因此當單擊該按鈕時,會為該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);
}
這個示例腳本怎么樣? 為了使用戶重定向到文件夾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();
}
<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>
如果我誤解了您的問題,對不起。
在以下腳本中,如果具有文件夾名稱的文件夾已存在,則它將打開現有文件夾。 如果該文件夾不存在,則創建具有文件夾名稱的文件夾。 為了使用此腳本,請修改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.