如何使用谷歌脚本将学生分数.txt数据解析到谷歌表

发布于 2025-01-11 01:47:18 字数 618 浏览 0 评论 0原文

我有多个 .txt 文件,其中包含每个科目的学生分数。

  1. 学生标记1
  2. 学生分数2

我想将学生分数解析到google使用谷歌脚本的工作表,输出应该是这样的。

  1. 学生的成绩单。

studentsheet image

我对列的范围有疑问,因为每个学生选修的科目数量不同。对于解析方法有什么建议吗?

sheet.getRange(lastRow +1,5,mark.length,marks[1].length).setValues(mark);

I have multiple .txt file that contains students' mark for each subject.

  1. students' mark1
  2. students' mark2

I want to parse the student mark to google sheet using google script and the output should be like this.

  1. Student's mark sheet.

student sheet image

I have a problem with the range of the column as each student they are taking a different number of subject. Is there any suggestion on the parsing method?

sheet.getRange(lastRow +1,5,mark.length,marks[1].length).setValues(mark);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2025-01-18 01:47:18

试试这个。代码更新。如果学生最多有 8 门科目。 (可以改为10或50)

  const file = DriveApp.getFileById('ID').getBlob().getDataAsString();
  const data = Utilities.parseCsv(file,'|');
  const arr = []
  const index = [] //0,7,12
  for(let i =0; i< data.length;i++){
    if(data[i].toString().includes('*')){
      index.push(i);
    }
  }
  const arr1 = []
  for (let i =0;i< index.length;i++){
      const start = index[i];
      const end = index[i+1]
      if(i+1 > index.length){
        arr1.push(data.slice(end))
      }else{
        arr1.push(data.slice(start,end))
      }
  }
  const finalarr = []
  for(let i=0; i< arr1.length;i++){
    const transit = []
    for(let j=0;j<arr1[i].length;j++){
       if(j == 0){
         transit.push(arr1[i][j][0],arr1[i][j][2])
       }else{
         transit.push(arr1[i][j][0].split(':')[1])
       }
    }
    //change the max subject length in here. Replace 8 with your desired numbers
    const arr_new = new Array(8)
    const arr_desired_length = transit.concat(arr_new.slice(transit.length))
    finalarr.push(arr_desired_length)
  }

  finalarr.forEach(r =>{
    if(r[0].includes('*')){
      r[0] = r[0].replace('*','')
    }
  })

  
  //sheet.getRange(lastRow +1,5,finalarr.length,finalarr[0].length).setValues(finalarr);
  console.log(finalarr)
  //OUTPUT in the same length
  [ [ 'ELMI ALSA', '89.0', '60', '88', '78', '68', '89', '70' ],
  [ 'EMMY JOE', '80.0', '86', '84', '89', '90', ,  ],
  [ 'ELIZA MOHRE', '73', '60', '70', , , ,  ] ]

Try this. Code Update. If student have max 8 subject. (you can change to 10 or 50)

  const file = DriveApp.getFileById('ID').getBlob().getDataAsString();
  const data = Utilities.parseCsv(file,'|');
  const arr = []
  const index = [] //0,7,12
  for(let i =0; i< data.length;i++){
    if(data[i].toString().includes('*')){
      index.push(i);
    }
  }
  const arr1 = []
  for (let i =0;i< index.length;i++){
      const start = index[i];
      const end = index[i+1]
      if(i+1 > index.length){
        arr1.push(data.slice(end))
      }else{
        arr1.push(data.slice(start,end))
      }
  }
  const finalarr = []
  for(let i=0; i< arr1.length;i++){
    const transit = []
    for(let j=0;j<arr1[i].length;j++){
       if(j == 0){
         transit.push(arr1[i][j][0],arr1[i][j][2])
       }else{
         transit.push(arr1[i][j][0].split(':')[1])
       }
    }
    //change the max subject length in here. Replace 8 with your desired numbers
    const arr_new = new Array(8)
    const arr_desired_length = transit.concat(arr_new.slice(transit.length))
    finalarr.push(arr_desired_length)
  }

  finalarr.forEach(r =>{
    if(r[0].includes('*')){
      r[0] = r[0].replace('*','')
    }
  })

  
  //sheet.getRange(lastRow +1,5,finalarr.length,finalarr[0].length).setValues(finalarr);
  console.log(finalarr)
  //OUTPUT in the same length
  [ [ 'ELMI ALSA', '89.0', '60', '88', '78', '68', '89', '70' ],
  [ 'EMMY JOE', '80.0', '86', '84', '89', '90', ,  ],
  [ 'ELIZA MOHRE', '73', '60', '70', , , ,  ] ]
不念旧人 2025-01-18 01:47:18

当我看到您的示例图像时,我猜测您当前的问题可能是由于放入电子表格的值中每个元素的数组长度所致。如果我的理解是正确的,那么下面的修改如何?

从:

sheet.getRange(lastRow +1,5,mark.length,marks[1].length).setValues(mark);

到:

const maxLen = Math.max(...mark.map(r => r.length));
const values = mark.map(r => r.length < maxLen ? [...r, ...Array(maxLen - r.length).fill("")] : r);
sheet.getRange(lastRow + 1, 5, values.length, values[1].length).setValues(values);
  • 在此修改中,值的每个长度都变为相同的长度。这样就可以使用setValues了。

注意:

  • 在您的显示脚本中,似乎使用了 markmarks 。从您的脚本中,我了解到 mark 是放入电子表格的值。

参考:

When I saw your sample image, I guessed that your current issue might be due to the array length of each element in the values for putting to the Spreadsheet. If my understanding is correct, how about the following modification?

From:

sheet.getRange(lastRow +1,5,mark.length,marks[1].length).setValues(mark);

To:

const maxLen = Math.max(...mark.map(r => r.length));
const values = mark.map(r => r.length < maxLen ? [...r, ...Array(maxLen - r.length).fill("")] : r);
sheet.getRange(lastRow + 1, 5, values.length, values[1].length).setValues(values);
  • In this modification, each length of your values becomes the same length. By this, setValues can be used.

Note:

  • In your showing script, it seems that mark and marks are used. From your script, I understood that mark is the value for putting to Spreadsheet.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文