CaiusNelson

Google game script line coder

Sep 27th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | Writing | 0 0
  1. function onOpen() {
  2. var ui = SpreadsheetApp.getUi();
  3. ui.createMenu('Game Script Functions')
  4. .addItem('Update Keys', 'updateKeysWithProgress') // Modify the menu item to use the new function
  5. .addToUi();
  6. }
  7.  
  8. function updateKeysWithProgress() {
  9. var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  10. var lastKeyColumnIndex = getColumnIndexByName(sheet, 'LastKey');
  11. var keyColumnIndex = getColumnIndexByName(sheet, 'Key');
  12. var sceneColumnIndex = getColumnIndexByName(sheet, 'Scene');
  13. var lineColumnIndex = getColumnIndexByName(sheet, 'Line');
  14.  
  15. var lastKey = null;
  16. var currentScene = null;
  17.  
  18. var totalRows = sheet.getLastRow() - 1; // Subtract 1 to exclude the header row
  19. var completedRows = 0;
  20.  
  21. for (var i = 2; i <= sheet.getLastRow(); i++) {
  22. var scene = sheet.getRange(i, sceneColumnIndex).getValue();
  23. var line = sheet.getRange(i, lineColumnIndex).getValue();
  24. var key = sheet.getRange(i, keyColumnIndex).getValue();
  25.  
  26. if (scene !== "") {
  27. currentScene = scene;
  28. lastKey = currentScene + "_-1";
  29. continue;
  30. }
  31.  
  32. if (line === "") {
  33. continue;
  34. }
  35.  
  36. if (line !== "" && key === "" && lastKey !== null) {
  37. var parts = lastKey.split('_');
  38. var index = parseInt(parts[1]) + 1;
  39. lastKey = parts[0] + "_" + index;
  40.  
  41. sheet.getRange(i, keyColumnIndex).setValue(lastKey);
  42. }
  43.  
  44. // Update progress
  45. completedRows++;
  46. var progress = (completedRows / totalRows) * 100;
  47.  
  48. // Show progress notification
  49. var notification = "Progress: " + Math.round(progress) + "% completed";
  50. SpreadsheetApp.getActiveSpreadsheet().toast(notification, "Progress", 1);
  51. }
  52. }
  53.  
  54. function getColumnIndexByName(sheet, columnName) {
  55. var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
  56. for (var i = 0; i < headers.length; i++) {
  57. if (headers[i] === columnName) {
  58. return i + 1;
  59. }
  60. }
  61. return -1;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment