简体   繁体   English

如何在谷歌应用脚本中获取用户输入?

[英]How to get user input in google app script?

Here is what I want to achieve:这是我想要实现的目标:

I want to delete 'n' number of rows from my google spreadsheet document.我想从我的谷歌电子表格文档中删除“n”行。 This 'n' can vary depending on number of wrong entries inserted in the document (I know this number before running the function) .这个“n”可以根据插入到文档中的错误条目的数量而变化(我在运行函数之前知道这个数字) And I want to give myself a flexibility to choose this number (just like console input in C, C++ or any other languages) .我想让自己灵活地选择这个数字(就像 C、C++ 或任何其他语言中的控制台输入一样)

Some researching shows solution via SpreadsheetApp.getUi() mode.一些研究通过SpreadsheetApp.getUi()模式显示解决方案。 But it is giving me error: Exception: Cannot call SpreadsheetApp.getUi() from this context.但这给了我错误: Exception: Cannot call SpreadsheetApp.getUi() from this context.

I don't want to open my spreadsheet as it is huge in size & takes time to load.我不想打开我的电子表格,因为它很大并且需要时间来加载。 Purpose of deleting rows pragmatically is that I don't have to open it, else its all 'moo' point.务实地删除行的目的是我不必打开它,否则它都是“moo”点。

Another solution could be to just create an variable and change is manually before running script.另一种解决方案可能是只创建一个变量并在运行脚本之前手动更改。 But it could create bad data if I forget to change that variable someday (I want to make it idiot-proof) .但是,如果有一天我忘记更改该变量,它可能会产生错误的数据(我想让它防白痴)

Is there any way to get user input for standalone google app script?有没有办法让用户输入独立的谷歌应用脚本? and without opening that particular google sheet?并且没有打开那个特定的谷歌表?

You can create a function in web app just write doGet() or doPost() function and call it with your input.您可以在 web 应用程序中创建 function 只需编写 doGet() 或 doPost() function 并使用您的输入调用它。 refer https://developers.google.com/apps-script/guides/web参考https://developers.google.com/apps-script/guides/web

Take input number of rows which is n in your case, and add your code to delete rows from SpreadSheet.输入您的情况为n的行数,并添加您的代码以从 SpreadSheet 中删除行。

you can pass input for get by using query parameter like:您可以使用查询参数传递输入以获得获取,例如:

?n=4

and you can use n in doGet() method.您可以在doGet()方法中使用 n 。

You can always put the script into a blank sheet and treat it as a placeholder for your functions and have the ui prompt pop there.您始终可以将脚本放入一张空白纸中,并将其视为您的函数的占位符,并在那里弹出 ui 提示符。 This way, you don't need to open your large sheet.这样,您无需打开大工作表。 You can always access other sheets when in another via Apps Script.当您在另一个工作表中时,您始终可以通过 Apps 脚本访问其他工作表。 This would be easier and you just need to transfer your script here.这会更容易,您只需在此处传输脚本。

Code:代码:

function showPrompt() {
  var ui = SpreadsheetApp.getUi(); 

  var result = ui.prompt(
      'Rows to delete?',
      'Input:',
      ui.ButtonSet.OK_CANCEL);

  var button = result.getSelectedButton();
  var numRows = result.getResponseText();
  if (button == ui.Button.OK) {
    // call function and pass the value
    deleteSheetRows(numRows);
  } 
}

function deleteSheetRows(numRows) {
  // url of the sheet with data
  var url = "https://docs.google.com/spreadsheets/d/***************/";
  var sheet = SpreadsheetApp.openByUrl(url);

  // do what you need to do here for that sheet using "numRows" value
  Logger.log("deleting "+numRows+" rows");
}

Output: Output:

迅速的

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM