2016-09-29 5 views
0

내 사이트에 이미 "CSV로 내보내기"버튼이 있습니다. 그러나 CSV를 Google 스프레드 시트에 직접 여는 'Google 스프레드 시트에서 열기'버튼을 사용하고 싶습니다."Google 스프레드 시트에서 열기"버튼을 만드는 방법

이렇게하면 사용자가 몇 단계를 저장하므로 더 이상 (1) CSV를 다운로드 할 필요가 없으며 (2) Google 스프레드 시트로 가져올 수 있습니다.

이 작업을 수행하는 방법은 무엇입니까?

답변

2

전체 API를 사용하지 않으려는 경우. . .

Google 스크립트를 사용하여 CSV를 Google 시트로 가져오고 합리적인 일정 (매분, 분, 시간, 일 등)으로 실행되도록 스크립트 트리거를 설정하십시오.

//adapted from http://stackoverflow.com/a/26858202/3390935 
function importData() { 
    var ss = SpreadsheetApp.getActive(); 
    var url = 'HTTP://YOURURL.COM/FILE.CSV'; 
    var file = UrlFetchApp.fetch(url); // get feed 

    var csv = file.getBlob().getDataAsString(); 
    var csvData = CSVToArray(csv); // see below for CSVToArray function 
    var sheet = ss.getActiveSheet(); 
// loop through csv data array and insert (append) as rows into 'NEWDATA' sheet 
for (var i=0, lenCsv=csvData.length; i<lenCsv; i++) { 
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i])); 
} 

}; 


// http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm 
// This will parse a delimited string into an array of 
// arrays. The default delimiter is the comma, but this 
// can be overriden in the second argument. 

function CSVToArray(strData, strDelimiter) { 
    // Check to see if the delimiter is defined. If not, 
    // then default to COMMA. 
    strDelimiter = (strDelimiter || ","); 

    // Create a regular expression to parse the CSV values. 
    var objPattern = new RegExp(
    (
     // Delimiters. 
     "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" + 

     // Quoted fields. 
     "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + 

     // Standard fields. 
     "([^\"\\" + strDelimiter + "\\r\\n]*))" 
    ), 
    "gi" 
); 

    // Create an array to hold our data. Give the array 
    // a default empty first row. 
    var arrData = [[]]; 

    // Create an array to hold our individual pattern 
    // matching groups. 
    var arrMatches = null; 

    // Keep looping over the regular expression matches 
    // until we can no longer find a match. 
    while (arrMatches = objPattern.exec(strData)){ 

    // Get the delimiter that was found. 
    var strMatchedDelimiter = arrMatches[ 1 ]; 

    // Check to see if the given delimiter has a length 
    // (is not the start of string) and if it matches 
    // field delimiter. If id does not, then we know 
    // that this delimiter is a row delimiter. 
    if (
     strMatchedDelimiter.length && 
     (strMatchedDelimiter != strDelimiter) 
    ){ 

     // Since we have reached a new row of data, 
     // add an empty row to our data array. 
     arrData.push([]); 

    } 

    // Now that we have our delimiter out of the way, 
    // let's check to see which kind of value we 
    // captured (quoted or unquoted). 
    if (arrMatches[ 2 ]){ 

     // We found a quoted value. When we capture 
     // this value, unescape any double quotes. 
     var strMatchedValue = arrMatches[ 2 ].replace(
     new RegExp("\"\"", "g"), 
     "\"" 
    ); 

    } else { 

     // We found a non-quoted value. 
     var strMatchedValue = arrMatches[ 3 ]; 

    } 

    // Now that we have our value string, let's add 
    // it to the data array. 
    arrData[ arrData.length - 1 ].push(strMatchedValue); 
    } 

    // Return the parsed data. 
     return(arrData); 
    }; 

는 시트 대중을 확인하고 URL에 /copy을 추가합니다.

https://docs.google.com/spreadsheets/d/YOURGDOCID/copy 
관련 문제