getFolderById 在应用程序编辑器中工作正常,但不会在相关电子表格中

问题描述 投票:0回答:1

我有一个旨在生成发票的电子表格。每张发票一张,以及一些用于管理内容的复选框。其中之一旨在将工作表作为 pdf 文件保存到同一帐户的 GDrive 的专用文件夹中。 这是我的代码:

function saveInvoiceIntoGDrivePDF() {

    const currentSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();

    const paramSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Paramètres généraux");

    const currentSheet = currentSpreadSheet.getActiveSheet();

    const invoicesStorageFolderOnGDriveId = paramSheet.getRange('B30').getValue();

    DriveApp.getFolderById(invoicesStorageFolderOnGDriveId);   const authorizationToken = ScriptApp.getOAuthToken();

    SpreadsheetApp.getUi().alert("Got Auth Token!");

    const exportLink = "https://docs.google.com/spreadsheets/d/"+currentSpreadSheet.getId()+"/export?format=pdf&size=a4&portrait=true&scale=4&top_margin=1.00&bottom_margin=1.00&left_margin=0.50&right_margin=0.50&gridlines=false&printnotes=false&pageorder=2&horizontal_alignment=CENTER&vertical_alignment=TOP&printtitle=false&sheetnames=false&fzr=false&fzc=false&attachment=false&r1=0&r2=40&c1=1&c2=6&gid="+currentSheet.getSheetId();

    const invoicingYear = SpreadsheetApp.getActiveSheet().getRange('J5').getValue();

    const invoicingMonth = SpreadsheetApp.getActiveSheet().getRange('N6').getValue();

    const customerName = SpreadsheetApp.getActiveSheet().getRange('J9').getValue();

    const indexFacture = SpreadsheetApp.getActiveSheet().getRange('J7').getValue();

    const invoicePDFFileName = "Invoice_"+invoicingYear+"_"+invoicingMonth+"_"+customerName+"_"+indexFacture+".pdf";

    const exportParams = {     method:"GET",     headers:{Authorization:"Bearer "+authorizationToken},     muteHttpExceptions:true   };

    var invoicePDFContent = UrlFetchApp.fetch(exportLink, exportParams).getBlob().setName(invoicePDFFileName);

    invoicesStorageFolderOnGDrive.createFile(invoicePDFContent);

}

将权限设置到 appsscript.json 中,如下所示:

{
    "timeZone": "Europe/Paris",
    "dependencies": {
    },
    "exceptionLogging": "STACKDRIVER",
    "runtimeVersion": "V8",
    "oauthScopes": [
        "https://www.googleapis.com/auth/script.external_request",
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/spreadsheets.readonly",
        "https://www.googleapis.com/auth/spreadsheets"
    ]
}

此功能与许多其他功能一样,由每个工作表“管理区域”中的复选框触发,标签为“在驱动器中另存为 PDF”。

问题是这个函数在从 Apps 脚本编辑器运行时就像一个魅力,但不会从 appsscript.json 收集驱动器权限。

我知道授权有限制,但我不明白的是,由于脚本项目(根据我的理解)attached 到电子表格,为什么它可以从编辑器运行但不能从电子表格运行是为什么而设计的?允许编写一个在“现实世界”中不起作用的脚本有什么好处?

而且,如果这是一个强授权问题,有没有办法让谷歌“接受”这个脚本,因为它只在它所属的同一个帐户中创建新文件?

感谢任何帮助, 非常感谢, 灵光

尽我所能从文档和论坛(但几乎无法获得“过时”的帖子)。

问题是这个电子表格将由对计算机知识很少的人运行,目前,他们必须:

  1. 单击“在单独的选项卡中打开 pdf”
  2. 下载pdf手动设置名字
  3. 将文件从他们的计算机上传到 GDrive

从他们的角度来看,这是(非常)痛苦的,而且非常令人满意,因为这是我“申请”的最后一行。

google-apps-script google-sheets google-drive-api authorization google-oauth
1个回答
0
投票

用于管理内容的复选框。其中之一旨在将工作表另存为 pdf

听起来您正在使用

onEdit(e)
简单的触发器来运行
saveInvoiceIntoGDrivePDF()
功能。

onEdit(e)
在无法访问任何个人信息或调用需要身份验证的服务的环境中运行。所有
DriveApp
方法都需要身份验证,因此不能从
onEdit(e)
.

调用它们

要使其工作,请使用 可安装的触发器 而不是简单的触发器,或通过其他方式运行

saveInvoiceIntoGDrivePDF()
功能,例如 按钮自定义菜单项侧边栏

© www.soinside.com 2019 - 2024. All rights reserved.