问题描述
我的目标是让多个用户同时更改文件而不会干扰其他人。我认为离线模式就是答案。这就是为什么我正在寻找一种在用户打开时打开离线模式编辑的方法。
也许离线模式不是最有效的方式,我愿意接受所有建议。
解决方法
您可以做的是复制工作表并让他们在复制的工作表上工作。使用下面的代码。
代码:
function openDuplicatedSheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetName = spreadsheet.getName();
// Duplicated file id
var fileId = SpreadsheetApp.create("Copy of " + spreadsheetName).getId();
var destination = SpreadsheetApp.openById(fileId);
// Rename sheet so it won't be duplicated
destination.renameActiveSheet("temp-sheet-to-be-removed-later)");
var sheets = spreadsheet.getSheets();
// Copy the sheets excluding the script so it won't duplicate infinitely
sheets.forEach(function (sheet,index) {
sheet.copyTo(destination);
// Use sheet name from the original sheet
var sheetName = sheet.getSheetName();
destination.getSheets()[index + 1].activate();
destination.renameActiveSheet(sheetName);
});
// Remove default Sheet1 which was renamed earlier to avoid duplication of sheet name
destination.deleteSheet(destination.getSheets()[0]);
var htmlOutput = HtmlService
.createHtmlOutput('<a href="https://docs.google.com/spreadsheets/d/' + fileId + '/" target="_blank">' + spreadsheetName + '</a>')
.setWidth(350)
.setHeight(50);
SpreadsheetApp.getUi().showModalDialog(htmlOutput,'Click to open duplicated sheet');
}
触发器:
打开时提示:
输出:
这将创建文件内容(不包括脚本)的副本/副本,并且应该可供他们自己的驱动器访问。
注意:
- 弹出窗口有一点延迟。等待它出现。
- 使用复制的文件来避免原始文件的多次重复。
- 复制的文件不会进一步复制,因为其脚本未包含在副本中。