Home > database >  App script keeps export csv file with double quotes
App script keeps export csv file with double quotes

Time:08-11

With this code, exported csv file has double quotes at each data.

How Can I export as CSV without double quotes??

function createDataUrl(type) {
  const mimeTypes = { csv: MimeType.CSV, pdf: MimeType.PDF };
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();


  var sheet_name = type;
  var file_name = ss.getSheetByName('설정').getRange('market').getValue()   sheet_name   '.csv';



  let url = null;
  url = `https://docs.google.com/spreadsheets/d/${ss.getId()}/gviz/tq?tqx=out:csv&gid=${ss.getSheetByName(sheet_name).getSheetId()}`;


  if (url) {
    const blob = UrlFetchApp.fetch(url, {
      headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
    }).getBlob();
    return {
      data:
        `data:${mimeTypes['csv']};base64,`  
        Utilities.base64Encode(blob.getBytes()),
      filename: file_name,
    };
  }
  return { data: null, filename: null };
}

CodePudding user response:

In your script, how about the following modification?

From:

const blob = UrlFetchApp.fetch(url, {
  headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
}).getBlob();

To:

const res = UrlFetchApp.fetch(url, { headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` } });
const blob = Utilities.newBlob(res.getContentText().replace(/"/g, ""), MimeType.CSV);
  • By this modification, " is removed from the returned text data.

Note:

  • In this modification, it supposes that each value of the CSV data doesn't include ". Please be careful about this.
  • Related