Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 我可以从google apps脚本生成的URL链接下载文件吗_Google Apps Script_Google Sheets_Google Apps - Fatal编程技术网

Google apps script 我可以从google apps脚本生成的URL链接下载文件吗

Google apps script 我可以从google apps脚本生成的URL链接下载文件吗,google-apps-script,google-sheets,google-apps,Google Apps Script,Google Sheets,Google Apps,请帮助我学习谷歌应用程序脚本的时间很短。 我想通过url从远程站点下载文件,url是从存储在电子表格中的数据生成的 例如,我有两个参数: Cell1 = val1, val2, ... valN Cell2 = val21, val22, ... val2N 我将字符串从单元格数据拆分到数组,然后生成URL。例如:http://mysite.com/files/file.val1.val22.zip 比我需要从这个链接下载文件 我可以自动执行此过程吗?此示例函数将检索您的zip文件,并将其放

请帮助我学习谷歌应用程序脚本的时间很短。 我想通过url从远程站点下载文件,url是从存储在电子表格中的数据生成的

例如,我有两个参数:

Cell1 = val1, val2, ... valN
Cell2 = val21, val22, ...  val2N
我将字符串从单元格数据拆分到数组,然后生成URL。例如:
http://mysite.com/files/file.val1.val22.zip
比我需要从这个链接下载文件


我可以自动执行此过程吗?

此示例函数将检索您的zip文件,并将其放入您的Google Drive文件夹“StackOverflow”中。您还可以从下载更完整的版本

例如:

getFile( "http://mysite.com/files/file.val1.val22.zip" );
请注意,由于您无法从应用程序脚本访问PC的资源(如文件系统),因此您无法自行下载。文件仍在“云中”。。。在本例中,它是从它所在的网站复制到Google Drive中的。但是,如果您正在运行驱动器应用程序,文件现在将同步到您的电脑。

是的,您可以。
希望下面的代码可以解决您的问题:

//Declare function
function downloadFile() {
  //Getting url,existing name and new name for image from the sheet in 
  //variable url, name and new_name respectively
  var sh = SpreadsheetApp.getActiveSheet();
  var row = sh.getLastRow();
  Logger.log(row);
  for (var i = 2; i <= row; i++) {

    var url = sh.getRange(i, 10).getValue();
    Logger.log(url);
    var name = sh.getRange(i, 13).getValue();
    var new_name = sh.getRange(i, 4).getValue();
    //Creating authentication token for downloading image, it may not be //required if image can be downloaded without login into
    var user = "***************";
    var password = "************";
    var headers = {
      "Accept": "application/xml",
      "Content-Type": "application/xml",
      "Authorization": "Basic " + Utilities.base64Encode(user + ":" + password)
    };
    //defining method to download file
    var options = {
      "method": "get",
      "headers": headers
    };
    //Getting folder name where to store downloaded image
    var folders = DriveApp.getFoldersByName('test');
    while (folders.hasNext()) {
      var folder = folders.next();
      Logger.log(folder.getName());
    }
    //Getting response on hit of url using downloading method defined //earlier storing in Blob
    var response = UrlFetchApp.fetch(url, options).getBlob();
    //Creating image in folder defined with response in blob and logging same file //in log to check, if required
    var file = folder.createFile(response);
    Logger.log(file);

    //renaming image
    var images = folder.getFiles();
    while (images.hasNext()) {
      var image = images.next();
      file.setName(new_name);
      Logger.log(image.getName());
    }
  }
}
//Hope you get it now
//声明函数
函数下载文件(){
//从中的工作表中获取图像的url、现有名称和新名称
//变量url、名称和新名称
var sh=SpreadsheetApp.getActiveSheet();
var row=sh.getLastRow();
Logger.log(行);
对于(var i=2;i它对我有效

function downloadFile() {
  var url = "https://raw.githubusercontent.com/hoat23/VisionArtificialAndImageProcessing/master/bin/utils_imgprocessing.py"
  Logger.log(url);
  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText()
  var newFile = DriveApp.createFile('testfilegoogle.txt',text);
  debugger;  // Stop to observe if in debugger
}
//Hope you get it now

下一个问题,如果服务器上不存在用于生成链接的文件,则代码get ERROR:UrlFetchApp.fetch(fileURL);并且脚本完成,我有错误代码:404。如何检查此错误?请检查作为要点发布的更完整的版本,按照我的答案中的链接进行操作。在该版本中,我添加了基本的HTTP响应检查;响应返回为“rc”。例如,
response=getFileFromURL(fileURL);if(response.rc!=200){//handle failure}
。我看到了您的示例,但字符串var response=UrlFetchApp.fetch(fileURL)中存在错误;这是我的代码示例,测试函数中的链接已断开,getFile()的第二行中存在错误函数
getResponseCode()
的文档意味着所有常见的HTTP错误都会被处理和返回,但情况似乎并非如此。重温要点,我添加了一个
try…catch
块以抑制错误,并插入一个404未找到的文件。(如果这有帮助,请随意更新此答案。)是的,现在它可以正常工作了……但我对谷歌的请求数量有一些权限。但这对我的需求仍然很有帮助。因为这是一个相当复杂的例子,你介意添加一些解释吗?它基本上是将图像链接存储在一张纸中,并在循环中逐个处理,下载数据存储在mydrive中创建的文件夹“test”中同时,它重命名了工作表的其他单元格中提供的图像名称。此外,我还提供了验证方法,以便在需要时验证下载。在代码中插入注释行
function downloadFile() {
  var url = "https://raw.githubusercontent.com/hoat23/VisionArtificialAndImageProcessing/master/bin/utils_imgprocessing.py"
  Logger.log(url);
  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText()
  var newFile = DriveApp.createFile('testfilegoogle.txt',text);
  debugger;  // Stop to observe if in debugger
}
//Hope you get it now