Here is what I want to achieve:
I want to delete n
rows from my Google Sheet. This n
can vary depending on the number of incorrect entries inserted in the document. (I know this number before running the function). And I want to give myself the flexibility to choose this number (just like console input in C, C++ or other languages).
Some researching shows a solution via SpreadsheetApp.getUi()
. But it is giving me the error: 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. The purpose of deleting rows pragmatically is that I don't have to open it, else its all a moot point.
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 a standalone Google Apps Script without opening that particular Google Sheet?
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. This way, you don't need to open your large sheet. You can always access other sheets when in another via Apps Script. This would be easier and you just need to transfer your script here.
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");
}