so i have a function duplicate sheet, but i also have another function that says getheetname. my getsheetname function is something that i have written down in A1, but the duplicatesheet function gets called when i press a draawing. what happens is that when i duplicate my sheet, the name of the sheet written in cell A1 is copy of template instead of the name that i give when calling the duplicatesheet function. how can i make sure that it writes down the name that i give up in the ui?
var nameofSheet; // create global variable, to use in multiple functions, and set a default name to it
function duplicateSheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template");
var response = SpreadsheetApp.getUi().prompt("Nieuwe sheet", "Wat moet de Titel van de nieuwe sheet worden?", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
if (response.getSelectedButton() == SpreadsheetApp.getUi().Button.OK && response.getResponseText() == "") {
// If the response is empty, call the function recursively
duplicateSheet();
} else if (response.getSelectedButton() == SpreadsheetApp.getUi().Button.OK && response.getResponseText() != "") {
// If the name is given, get the name of the sheet and log it
nameofSheet = response.getResponseText();
console.log("Dit is de sheetnaam: " + nameofSheet);
// Create a new sheet and set its name
var newSheet = sheet.copyTo(sheet.getParent()).setName(nameofSheet);
// Copy values and formats
sheet.getDataRange().copyTo(newSheet.getRange(1, 1));
// Get merged cell ranges from the original sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:F50");
var mergedRanges = range.getMergedRanges();
// Copy and log merged cells
for (var i = 0; i < mergedRanges.length; i++) {
var mergedRange = mergedRanges[i];
var newMergedRange = newSheet.getRange(
mergedRange.getRow(),
mergedRange.getColumn(),
mergedRange.getNumRows(),
mergedRange.getNumColumns()
);
newMergedRange.merge();
console.log(mergedRanges[i].getA1Notation());
console.log(mergedRanges[i].getDisplayValue());
}
} else {
throw("Je hebt het maken van een nieuwe sheet geannuleerd");
}
}
/**
* Haalt de naam van de sheet op
*
* @customfunction
*/
function getSheetName() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
console.log("sheet name: " + sheet.getName())
return sheet.getName();
i have tried everything and a lot of different ways to call the function with delays etc., but noting works.
how can i make sure that it writes down the name that i give up in the ui?
This line:
sheet.getDataRange().copyTo(newSheet.getRange(1, 1));
Should be:
sheet.getDataRange().copyTo(newSheet.getDataRange())
Or at the very least:
newSheet.getRange("A1").setValue(nameofSheet)