gpt4 book ai didi

google-apps-script - 使用 pdf 选项使用 google 脚本将新版本的 google 电子表格导出(或打印)为 pdf 文件

转载 作者:行者123 更新时间:2023-12-04 13:32:21 25 4
gpt4 key购买 nike

我正在尝试使用页面参数(纵向/横向,...)

我对此进行了研究并找到了可能的解决方案 here .
有几个类似的解决方案,但只适用于旧版本的谷歌电子表格。

请考虑以下代码:

function exportAsPDF() {
//This code runs from a NEW version of spreadsheet

var oauthConfig = UrlFetchApp.addOAuthService("google");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous"); oauthConfig.setConsumerSecret("anonymous");
var requestData = { "method": "GET", "oAuthServiceName": "google","oAuthUseToken": "always" };

var ssID1="0AhKhywpH-YlQdDhXZFNCRFROZ3NqWkhBWHhYTVhtQnc"; //ID of an Old version of spreadsheet
var ssID2="10xZX9Yz95AUAPu92BkBTtO0fhVk9dz5LxUmJQsJ7yPM"; //ID of a NEW version of spreadsheet

var ss1 = SpreadsheetApp.openById(ssID1); //Old version ss object
var ss2 = SpreadsheetApp.openById(ssID2); //New version ss object
var sID1=ss1.getActiveSheet().getSheetId().toString(); // old version sheet id
var sID2=ss2.getActiveSheet().getSheetId().toString(); // new version sheet id

//For Old version, this runs ok.
var url1 = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+ssID1+"&gid="+sID1+"&portrait=true"+"&exportFormat=pdf";
var result1 = UrlFetchApp.fetch(url1 , requestData);
var contents1=result1.getBlob();
var pdfFile1=DriveApp.createFile(contents1).setName("FILE1.pdf");

//////////////////////////////////////////////
var url2 = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+ssID2+"&gid="+sID2+"&portrait=true"+"&exportFormat=pdf";
var result2 = UrlFetchApp.fetch(url2 , requestData);
var contents2=result2.getBlob();
var pdfFile2=DriveApp.createFile(contents2).setName("FILE2.pdf");
}

它工作正常并生成文件“FILE1.pdf”,可以正确打开。但是对于新版本的电子表格,它会在“var result2 = UrlFetchApp.fetch(url2 , requestData);”处导致错误 302(截断的服务器响应)。嗯,没关系,因为新版本的 url 格式不包含“key”参数。新版本的正确 url 必须类似于 "https://docs.google.com/spreadsheets/d/"+ssID2+"/export?gid="+sID2+"&portrait=true&format=pdf"
将此用于 url2 ( var url2 = "https://docs.google.com/spreadsheets/d/"+ssID2+"/export?gid="+sID2+"&portrait=true&format=pdf" ) 它再次失败并显示错误“无法为服务执行授权:google”。
好吧,这个错误可能是由于 RequestTokenUrl 的范围不正确。我找到了替代范围 https://docs.google.com/feeds并设置它: oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feed/");代码再次运行后,在 UrlFetchApp.fetch(url2 , requestData); 行发生了新的错误。 :“错误 OAuth”……我不知道如何继续……我已经测试了数百个变体,但没有很好的结果。

有任何想法吗?新版电子表格的范围 docs.google.com/feeds 是否正确? oauthConfig 是否正确?

提前致谢。

最佳答案

这是我的电子表格到 pdf 脚本。它适用于新的 Google 电子表格 API。

// Convert spreadsheet to PDF file.
function spreadsheetToPDF(id,index,url,name)
{
SpreadsheetApp.flush();

//define usefull vars
var oauthConfig = UrlFetchApp.addOAuthService("google");
var scope = "https://docs.google.com/feeds/";

//make OAuth connection
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");

//get request
var request = {
"method": "GET",
"oAuthServiceName": "google",
"oAuthUseToken": "always",
"muteHttpExceptions": true
};

//define the params URL to fetch
var params = '?gid='+index+'&fitw=true&exportFormat=pdf&format=pdf&size=A4&portrait=true&sheetnames=false&printtitle=false&gridlines=false';

//fetching file url
var blob = UrlFetchApp.fetch("https://docs.google.com/a/"+url+"/spreadsheets/d/"+id+"/export"+params, request);
blob = blob.getBlob().setName(name);

//return file
return blob;
}

我不得不使用“muteHttpExceptions”参数来准确地知道新的 URL。使用此参数,我下载了带有 HTML 扩展名的文件,以获取带有最终 URL 的“永久移动”页面(“ https://docs.google.com/a/“+url+”/spreadsheets/d/“+id+”/export“+params”)。

请注意,我在一个组织中。所以我不得不指定它的域名(“url”参数,即“mydomain.com”)。

关于google-apps-script - 使用 pdf 选项使用 google 脚本将新版本的 google 电子表格导出(或打印)为 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21997924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com