Reputation: 129
I am trying to export a csv from a Google sheet and give it the name entered into a dialog box. the code is:
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheet_Name = "csv"
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_Name)
var sheetNameId = sheet.getSheetId().toString();
params= ssID+"/export?gid="+sheetNameId +"&format=csv"
var url = "https://docs.google.com/spreadsheets/d/"+ params
var result = UrlFetchApp.fetch(url, requestData);
var ui = SpreadsheetApp.getUi();
var result = ui.prompt("Enter Name for CSV");
var csvName = result.getResponseText();
var folder_id ='1ENj9Rk0djm2jp0FOorWcWmsIEqEp1pFp';
var timeZone = Session.getScriptTimeZone();
date = Utilities.formatDate(new Date(), timeZone, "YYMMdd");
var resource = {
title: date+csvName+".csv",
mimeType: "application/vnd.csv",
parents: [{ id: folder_id }]}
var fileJson = Drive.Files.insert(resource, result, {supportsAllDrives: true});
Problem is I just get:
Exception: The mediaData parameter only supports Blob types for upload.
Is this an easy fix??
Upvotes: 2
Views: 1107
Reputation: 201503
In your script, result
of var result = UrlFetchApp.fetch(url, requestData);
is overwritten by var result = ui.prompt("Enter Name for CSV");
. I think that this is the reason of your issue. So in your script, how about the following modification?
var result = ui.prompt("Enter Name for CSV");
var csvName = result.getResponseText();
var res = ui.prompt("Enter Name for CSV");
var csvName = res.getResponseText();
Upvotes: 2