【问题标题】:Overwrite Spreadsheet in Google Apps Script在 Google Apps 脚本中覆盖电子表格
【发布时间】:2013-06-11 05:21:55
【问题描述】:

我想在每次执行脚本 (GAS) 时打开一个新的电子表格。不幸的是,SpreadsheetApp.create() 不断创建同名文件的“多个”副本。如何覆盖驱动器上已存在的文件内容?

【问题讨论】:

  • 您的问题对我来说有点不清楚:您要打开 还是现有 电子表格? .create(name) 根据您的要求命名文件 - 如果您想要不同的名称,您应该在每次调用时指定不同的名称。如果你想覆盖现有文件,你应该避免使用.create(),而是使用.openById()
  • 你能把你写的代码贴出来。代码胜于雄辩

标签: google-apps-script google-sheets


【解决方案1】:

您可以使用SpreadsheetApp.create() 来创建一个具有给定名称的新电子表格文件,但没有等效的SpreadsheetApp.open() 函数可以接受文件名。正如您所发现的,.create()始终创建一个新文件 - 它不关心同名文件是否已经存在。

如果要打开文件名来打开电子表格,则需要先找到该文件,然后将File Object 传递给SpreadsheetApp.open()

此实用程序函数将打开一个现有的电子表格,或者在不存在此类文件时创建一个。

/**
 * Returns the Spreadsheet with the given file name. If no such spreadsheet
 * exists, it will be created.
 */
function openSheetByName(filename) {
  if (arguments.length == 0 || filename =="") throw new Error( "Missing filename." );

  var files = DocsList.getFilesByType('spreadsheet');
  var sheet;
  var i = 0;
  // Loop over all spreadsheet files. Loop ends when we reach the end,
  // or if we find a matching filename.
  while ( i < files.length && (files[i].getName() !== filename) )
    i++;

  if (i == files.length) {
    // We didn't find the file, so create it.
    sheet = SpreadsheetApp.create(filename);
  }
  else {
    // We found it, use it.
    sheet = SpreadsheetApp.open(files[i]);
  }
  return sheet;
}

这是使用实用函数的 readRows() 示例的修改版本。

function readRows() {
  var sheet = openSheetByName("Favourite");

  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    Logger.log(row);
  }
};

【讨论】:

    【解决方案2】:

    使用最新版本的 Google Apps 服务,@Mogsdad 提供的用于打开现有工作表的脚本可以被修改为打开现有工作表或创建一个新工作表(如果它不存在)

    /**
    * Returns the Spreadsheet with the given file name. If no such spreadsheet
    * exists, it will be created.
    */
    function openSheetByName(filename) 
    {
        if (arguments.length == 0 || filename =="") throw new Error( "Missing filename." );
    
            var files = DriveApp.getFilesByName(filename);
            var sheet;
            // Check we found a sheet with the name
            while ( files.hasNext())
            {
                sheet = files.next();
                if(sheet.getName() == filename)
                {
                    Logger.log("Opened Sheet: " + filename);
                    return SpreadsheetApp.open(sheet);
                }
           }
    
          // We didn't find the file, so create it.
          sheet = SpreadsheetApp.create(filename);
          Logger.log("Created new Sheet for: " + filename);
          return sheet;
    }
    

    【讨论】:

      【解决方案3】:

      您可能正在寻找Sheet.clear()

      删除内容和格式化here的选项很少。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多