You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Script报错:Iterator已到末尾,非创建者无法生成CSV

问题:非表格创建者执行Google表格脚本生成CSV时触发权限错误

我在Google表格中使用脚本,基于指定单元格范围(A2:AB3)在表格所在文件夹生成CSV文件,绑定按钮触发。但非表格创建者账号点击按钮时,会报错:

Exception: Cannot retrieve the next object: iterator has reached the end.

已确认该账号具备文件夹编辑权限,寻求解决方法支持其他账号生成CSV。

原脚本代码:

/** 
     * Create CSV file of Sheet2
     * Modified script written by Tanaike
     * https://stackoverflow.com/users/7108653/tanaike
     * 
     * Additional Script by AdamD.PE
     * version 13.11.2022.1
     * https://support.google.com/docs/thread/188230855
     */

    const date = new Date();

    /** Extrai a data de hoje */
    let day = date.getDate();
    let month = date.getMonth() + 1;
    let year = date.getFullYear();


    if (day < 10) {
        day = '0' + day;
    }

    if (month < 10) {
        month = `0${month}`;
    }

    let currentDate = `${day}-${month}-${year}`;


    function sheetToCsvModelo0101() {
      var filename = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetName() + "-01" + " - " + currentDate; // CSV file name
      filename = filename + '.csv';
      var ssid=SpreadsheetApp.getActiveSpreadsheet().getId();
      var saveTo = DriveApp.getFileById(ssid).getParents().next().getId();
      
      // Tanaike script to create csv file
      var csv = "";
      var v = SpreadsheetApp
              .getActiveSpreadsheet()
              .getActiveSheet()
              .getRange("A2:AB3")
              .getValues();
      v.forEach(function(e) {
        csv += e.join(",") + "\n";
      });

      var newDoc = DriveApp.createFile(filename, csv, MimeType.CSV);
      var file = DriveApp.getFileById(newDoc.getId());
      DriveApp.getFolderById(saveTo).addFile(file);
      DriveApp.getRootFolder().removeFile(file);
    } 

错误原因

报错触发点为DriveApp.getFileById(ssid).getParents().next():非表格创建者账号虽有表格编辑权限,但仅通过表格共享继承文件夹权限,无法通过DriveApp.getParents()方法直接获取父文件夹,导致迭代器为空,调用next()时抛出异常。

修复方案

使用SpreadsheetApp.getActiveSpreadsheet().getDriveFolder()替代原获取父文件夹的逻辑,该方法允许有表格编辑权限的用户直接获取所在文件夹,无需额外的文件夹直接权限。同时优化文件移动逻辑,减少冗余操作。

修复后的完整代码:

/** 
 * Create CSV file of Sheet2
 * Modified script written by Tanaike
 * Additional Script by AdamD.PE
 * version 13.11.2022.1
 */

// 提取当前日期(格式:DD-MM-YYYY)
const getCurrentDate = () => {
  const date = new Date();
  const day = date.getDate().toString().padStart(2, '0');
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const year = date.getFullYear();
  return `${day}-${month}-${year}`;
};

function sheetToCsvModelo0101() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const activeSheet = ss.getActiveSheet();
  
  // 生成CSV文件名
  const filename = `${activeSheet.getSheetName()}-01 - ${getCurrentDate()}.csv`;
  
  // 获取表格所在文件夹(核心修复点)
  const targetFolder = ss.getDriveFolder();
  
  // 生成CSV内容
  const dataRange = activeSheet.getRange("A2:AB3");
  const csvContent = dataRange.getValues()
    .map(row => row.join(","))
    .join("\n");
  
  // 创建CSV并直接移动到目标文件夹
  targetFolder.createFile(filename, csvContent, MimeType.CSV);
}

补充说明

  • 确保非创建者账号拥有该Google表格的编辑权限;
  • 原按钮绑定逻辑无需修改,非创建者点击按钮即可正常生成CSV到表格所在文件夹。

内容的提问来源于stack exchange,提问作者Tyrone Hirt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:25:32