首页 > 解决方案 > 删除触发器自动删除了我的脚本项目

问题描述

当我删除我唯一的触发器(设置为每天运行)时,它占用了我所有的脚本代码!

当我现在进入工具/脚本编辑器时,它显示一个空白项目(不是我已经工作了一个月的保存代码!)

恢复以前版本的电子表格不起作用,我正在使用我创建它的帐户。

我不知道表格自动将 .gs 文件存储在哪里(它们不会显示在我的云端硬盘上),但我希望它仍然存在于他们的服务器上,只是电子表格中指向它的链接已损坏并且可以恢复.

请帮忙,因为我不想从头开始

标签: google-apps-script

解决方案


这是一个脚本,用于将我的文件备份为单独的文件和一个大的 JSON 文件。它不会帮助您解决当前的问题,但您可以在将来使用它来避免它,并且与备份整个电子表格和创建另一个不必要的项目不同,它们被保存为 ascii 文本文件。

function saveScriptBackupsDialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('backupscripts1'), 'Script Files Backup Dialog');
}

function scriptFilesBackup(obj) {
  console.log(JSON.stringify(obj));
  const scriptId = obj.script.trim();
  const folderId = obj.folder.trim();
  const saveJson = obj.saveJson;
  const saveFiles = obj.saveFiles;
  const fA = obj.selected;
 
  if (scriptId && folderId) {
    const base = "https://script.googleapis.com/v1/projects/"
    const url1 = base + scriptId + "/content";
    const url2 = base + scriptId;
    const options = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
    const res1 = UrlFetchApp.fetch(url1, options);
    const data1 = JSON.parse(res1.getContentText());
    const files = data1.files;
    const folder = DriveApp.getFolderById(folderId);

    const res2 = UrlFetchApp.fetch(url2, options);
    const data2 = JSON.parse(res2.getContentText());
    let dts = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyyMMdd-HH:mm:ss");
    let subFolderName = Utilities.formatString('%s-%s', data2.title, dts);
    let subFolder = folder.createFolder(subFolderName);
    if (saveFiles) {
      files.forEach(file => {
        if (file.source && file.name) {
          let ext = (file.type == "HTML") ? ".html" : ".gs";
          if (~fA.indexOf(file.name)) {
            subFolder.createFile(file.name + ext, file.source, MimeType.PLAIN_TEXT)
          }
        }
      });
    }
    if (saveJson) {
      subFolder.createFile(subFolderName + '_JSON', res1, MimeType.PLAIN_TEXT)
    }
  }
  return { "message": "Process Complete" };
}

对话框的 html:

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
    <style>
        input {margin: 2px 5px 2px 0;}
    #btn3,#btn4{display:none}
    </style>
</head>

<body>
    <form>
        <input type="text" id="scr" name="script" size="60" placeholder="Enter Apps Script Id" onchange="getFileNames();" />
        <br /><input type="text" id="fldr" name="folder" size="60" placeholder="Enter Backup Folder Id" />
        <div id="shts"></div>
    <br /><input type="button" value="0" onClick="unCheckAll();" size="15" id="btn3" />
    <input type="button" value="1" onClick="checkAll();"size="15" id="btn4"/>
        <br /><input type="checkbox" id="files" name="saveFiles" checked /><label for="files">Save Files</label>
        <br /><input type="checkbox" id="json" name="saveJson" checked /><label for="json">Save JSON</label>
        <br /><input type="button" value="Submit" onClick="backupFiles();" />
    </form>
        <script>
      function getFileNames() {
        const scriptid = document.getElementById("scr").value;
        google.script.run
        .withSuccessHandler((names) => {
          document.getElementById('btn3').style.display = "inline";
          document.getElementById('btn4').style.display = "inline";
          names.forEach((name,i) => {
           let br = document.createElement("br"); 
           let cb = document.createElement("input");
           cb.type = "checkbox";
           cb.id = `cb${i}`;
           cb.name = `cb${i}`;
           cb.className = "cbx";
           cb.value = `${name}`;
           cb.checked = true;
           let lbl = document.createElement("label");
           lbl.htmlFor = `cb${i}`;
           lbl.appendChild(document.createTextNode(`${name}`));
           document.getElementById("shts").appendChild(cb);
           document.getElementById("shts").appendChild(lbl);
           document.getElementById("shts").appendChild(br);
          });
        })
        .getAllFileNames({scriptId:scriptid}); 
      }
      function unCheckAll() {
        let btns = document.getElementsByClassName("cbx");
        console.log(btns.length);
        for(let i =0;i<btns.length;i++) {
          btns[i].checked = false;
        }
      }
      function checkAll() {
        let btns = document.getElementsByClassName("cbx");
        console.log(btns.length)
        for(let i = 0;i<btns.length;i++) {
          btns[i].checked = true; 
        }
      }
      function backupFiles() {
        console.log('backupFiles');
        sObj = {};
        sObj.script = document.getElementById('scr').value;
        sObj.folder = document.getElementById('fldr').value;
        sObj.saveJson = document.getElementById('json').checked?'on':'';
        sObj.saveFiles = document.getElementById('files').checked?'on':'';
        sObj.selected = [];
        console.log("1");
        const cbs = document.getElementsByClassName("cbx");
        let selected = [];
        for(let i = 0;i<cbs.length; i++) {
          let cb = cbs[i];
          if(cb.checked) {
            sObj.selected.push(cb.value)
          } 
        }
        console.log("2");
        google.script.run
        .withSuccessHandler(function(obj){google.script.host.close();})
        .scriptFilesBackup(sObj);
        console.log(JSON.stringify(sObj));
      }
        </script>
</body>

</html>

如果你想恢复只是问。我没有太多使用它,我通常使用我需要的文件并将其粘贴过去。


推荐阅读