上传文件按钮

时间:2019-06-18 23:02:40

标签: google-apps-script google-sheets

我想在Google电子表格中有一个简单的按钮,它将所选文件上传到Google云端硬盘上的特定文件夹。不应用谷歌表格。只需一个上传按钮。有人为此任务提供一些脚本代码示例吗?

1 个答案:

答案 0 :(得分:0)

我了解您希望SpreadSheet中的按钮将文件从PC上载到Google云端硬盘。 第一步是创建一个上传脚本。您可以使用Google Drive API文档中的sample

function insertFile(fileData, callback) {
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = fileData.type || 'application/octet-stream';
    var metadata = {
      'title': fileData.fileName,
      'mimeType': contentType
    };

    var base64Data = btoa(reader.result);
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    var request = gapi.client.request({
        'path': 'your path file',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody});
    if (!callback) {
      callback = function(file) {
        console.log(file)
      };   }    request.execute(callback);  }}

然后,您必须在工作表中创建按钮。为此,请转至插入>绘图(如果有,则为图片),然后绘制自定义按钮。 完成后,右键单击按钮并选择“分配脚本”,然后输入已保存脚本的名称。

相关问题