I have a script that runs but before final step of deleting some rows I wanted the user to be able to select yes or no, however when I run the code the alert box randomly pops up before the entire code runs. The alert box code is at the bottom of my function.
At first I put the variables at the very top of the function which made the alert box pop up first so I moved it to the very bottom thinking it would resolve the issue but there is still code before the alert that hasn't run before it asks for input.
The logger notes are just while I'm trying to figure out the code but I need the code to sort column 8 before the pop-up because if yes is selected it will delete all the rows with a value under 1500 in that column and I want them to be able to visually see which rows those are.
function myFunction() {
// Set Variables
var cs = SpreadsheetApp.getActiveSheet();
var lr = cs.getLastRow();
// Delete Rows
cs.deleteRow(lr);
cs.deleteRows(1,6);
// Separate List
cs.getRange("A1:A100").splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.SPACE);
cs.sort(8,false)
// Set More Variables
var Hrng = cs.getRange("H1:H10");
var Hvals = Hrng.getValues();
// Organize List
for (var i=0;i<10;i++){
var val = Hvals[i][0];
var type = (typeof val)
if (type == "string"){
continue
}
else {
break
}
}
// Final Cleanup
cs.getRange('G1:G'+i).deleteCells(SpreadsheetApp.Dimension.COLUMNS);
cs.sort(8,true);
// Alert Box
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Are you sure you want to continue?', ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
Logger.log('The user clicked "Yes."');} else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
Try adding a line with SpreadsheetApp.flush()
before the Alert line. It should "wait"until the previous code is finished