Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onOpen() {
- var ui = SpreadsheetApp.getUi();
- ui.createMenu('Game Script Functions')
- .addItem('Update Keys', 'updateKeysWithProgress') // Modify the menu item to use the new function
- .addToUi();
- }
- function updateKeysWithProgress() {
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- var lastKeyColumnIndex = getColumnIndexByName(sheet, 'LastKey');
- var keyColumnIndex = getColumnIndexByName(sheet, 'Key');
- var sceneColumnIndex = getColumnIndexByName(sheet, 'Scene');
- var lineColumnIndex = getColumnIndexByName(sheet, 'Line');
- var lastKey = null;
- var currentScene = null;
- var totalRows = sheet.getLastRow() - 1; // Subtract 1 to exclude the header row
- var completedRows = 0;
- for (var i = 2; i <= sheet.getLastRow(); i++) {
- var scene = sheet.getRange(i, sceneColumnIndex).getValue();
- var line = sheet.getRange(i, lineColumnIndex).getValue();
- var key = sheet.getRange(i, keyColumnIndex).getValue();
- if (scene !== "") {
- currentScene = scene;
- lastKey = currentScene + "_-1";
- continue;
- }
- if (line === "") {
- continue;
- }
- if (line !== "" && key === "" && lastKey !== null) {
- var parts = lastKey.split('_');
- var index = parseInt(parts[1]) + 1;
- lastKey = parts[0] + "_" + index;
- sheet.getRange(i, keyColumnIndex).setValue(lastKey);
- }
- // Update progress
- completedRows++;
- var progress = (completedRows / totalRows) * 100;
- // Show progress notification
- var notification = "Progress: " + Math.round(progress) + "% completed";
- SpreadsheetApp.getActiveSpreadsheet().toast(notification, "Progress", 1);
- }
- }
- function getColumnIndexByName(sheet, columnName) {
- var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
- for (var i = 0; i < headers.length; i++) {
- if (headers[i] === columnName) {
- return i + 1;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment