gpt4 book ai didi

csv - 将数据从 Google Drive 中的 CSV 文件导入到 Google Sheet

转载 作者:行者123 更新时间:2023-12-04 21:06:46 29 4
gpt4 key购买 nike

我正在使用 SAS 每 24 小时生成两个 CSV 文件。我使用 bat 脚本将生成的 CSV 文件保存在 Google Drive 的文件夹中。 CSV 文件已被替换,因此文件夹中将始终只有这两个文件。

CSV 文件以“,”分隔,仅包含三列或四列。

我想在 Google 表格和 CSV 文件之间创建直接链接,以便 Google 表格自动更新为最新数字。

我尝试过使用“ImportData”功能但没有成功

=IMPORTDATA("https://drive.google.com/file/d/123231jshu231731/edit?usp=sharing")

其中 123231jshu231731 是 file_id。

但我得到的错误是
Result was not automatically expanded, please insert more columns (896).

这没有意义,因为文件只有 3 列

希望有人能更好地解决我的自动化问题

谢谢

最佳答案

将 csv 文件发布到网络

显然 IMPORTDATA仅适用于发布到网络的文档。您可以通过两种方式让它为您工作:

  • Store your csv files in a public google drive folder
  • 以其他方式在网络上托管您的 csv 文件 - 我想您需要在您的机器上使用 csv 文件运行服务器。

  • 但是,这涉及将您的 csv 数据公开给任何连接到互联网的人(可能是您不想要的)

    将 csv 文件保密

    如果您希望您的数据保持私密,您可以制作一个脚本,定期将您的 csv 数据加载到电子表格中。

    Google 提供服务电话 Google Apps Script (GAS) 允许您用 JavaScript 编写脚本以在谷歌驱动器中执行更复杂的任务。

    在 GAS 中,你可以创建一个叫做 installable trigger 的东西。您可以将其设置为在设定的时间间隔内定期运行。

    这是我将如何编写脚本以上传您的 csv 数据的基本布局:
    function timedTrigger() {
    // list the csv file ids
    var baseballScoresFileId = "123231jshu231731";
    var donutPricesFileId = "984732ageyn555646";

    // get the spreadsheet by it's id
    var ss = SpreadsheetApp.openById("the spreadsheet id");

    // get the sheets you want to print the data to
    var baseballSheet = ss.getSheetByName("Baseball Scores");
    var donutsSheet = ss.getSheetByName("Donuts Scores");

    // print the data to the first row first columnscript
    printCsvData(baseballScoresFileId, baseballSheet, 1, 1);

    printCsvData(donutPricesFileId, donutsSheet, 5, 2); // prints to B5
    }

    // This function loads the data from a csv fileId
    // and prints it to the sheet starting at the cell
    // located at (row, col)
    // It works just like IMPORTDATA except you specify the row and col in the
    function printCsvData(fileId, sheet, row, col) {
    // get data from file
    var data = DriveApp.getFileById(fileId).getBlob().getDataAsString();

    // parse the csv string into two dimensional array
    var values = parseCsvData(data);

    // print the values into the range starting at row, col
    sheet.getRange(row, col, values.length, values[0].length).setValues(values);
    }

    // This function converts a string of comma
    // separated values and converts them into
    // a two dimensional array
    function parseCsv(string) {
    var values = [];
    // ...
    return values;
    }

    我不太确定你是如何格式化你的 CSV 文件的;我最好的猜测是:
    function parseCSV(string) {
    return string.split("\n").map(function (row) {
    return row.split(",");
    });
    }

    您可能需要担心额外的空白。

    如果您不熟悉 JavaScript 或编程,这将是一个很好的学习机会。但是,如果加载 csv 文件不是一个迫切需要,请注意,您可能需要 5-20 个小时来了解如何设置脚本。不管怎样,祝你好运!

    关于csv - 将数据从 Google Drive 中的 CSV 文件导入到 Google Sheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41282250/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com